Using
PHP to get and display database data function mysql_result()
mixed mysql_result(resource result_set, int row [,mixed field])
Get the data of a field from the specified row of result_set. Simple but inefficient.
Example:
Get the entire row from result_set and put the data into the array.
Example (note the clever combination with list):
- for ($i = 0; $i <= mysql_num_rows($result); $i++)
- {
- $id = mysql_result($result, 0, "id");
- $name = mysql_result($result, 0, "name");
- echo "Product: $name ($id)";
- }
PHP gets and displays database data function mysql_fetch_array()
array mysql_fetch_array(resource result_set [,int result_type])
Enhancement of mysql_fetch_row() version.
Get each row of result_set as an associative array or/and a numerical index array.
Get two arrays by default, result_type can be set:
- $query = "select id,
name from product order by name";- $result = mysql_query($query);
- while(list($id, $name)
= mysql_fetch_row($result)) {- echo "Product: $name ($id)";
- }
MYSQL_BOTH: Gets two arrays. Therefore, each field can be referenced by index offset or by field name.Example:
PHP gets and displays database data function mysql_fetch_assoc()
array mysql_fetch_assoc(resource result_set)
- $query = "select id,
name from product order by name";- $result = mysql_query($query);
- while($row = mysql_fetch_array
($result, MYSQL_BOTH)) {- $name = $row['name'];
- //或者 $name = $row[1];
- $name = $row['id'];
- //或者 $name = $row[0];
- echo "Product: $name ($id)";
- }
object mysql_fetch_object(resource result_set)It has the same function as mysql_fetch_array(), but it returns not an array, but It is an object.
Example:
The above functions are a summary of PHP's functions for obtaining and displaying database data.
http://www.bkjia.com/PHPjc/446138.html
- $query = "select id, name
from product order by name";- $result = mysql_query($query);
- while($row = mysql_fetch_object
($result)) {- $name = $row->name;
- $name = $row->id;
- echo "Product: $name ($id)";
- }
true
TechArticle