How to correctly understand PHP's function for obtaining and displaying database data. Obtain the number of fields in the specified row of result_set using mysql_result () mixedmysql_result (resourceresult_set, introw [, mixedfield]) of mysql_result () used to display database data functions.
PHP mysql_result () for obtaining the function for displaying database data ()
Mixed mysql_result (resource result_set, int row [, mixed field])
Obtains the data of a field from the specified row of result_set. this is simple but inefficient.
Example:
- $ Link1 = @ mysql_connect ("server1 ",
"Webuser", "password ")
- Or die ("cocould not connect
To mysql server! ");
- @ Mysql_select_db ("company ")
Or die ("cocould not select database! ");
- $ Query = "select id, name
From product order by name ";
- $ Result = mysql_query ($ query );
- $ Id = mysql_result ($ result, 0, "id ");
- $ Name = mysql_result ($ result, 0, "name ");
- Mysql_close ();
Note that the above code is only the field value of the first data in the output result set. if you want to output all records, you need to process them cyclically.
- 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)";
- }
Note: If the field name is an alias, the alias is used in mysql_result.
PHP mysql_fetch_row ()
Array mysql_fetch_row (resource result_set)
Obtain the entire row from result_set and put the data into the array.
For example ):
- $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)";
- }
PHP mysql_fetch_array ()
Array mysql_fetch_array (resource result_set [, int result_type])
Enhanced version of mysql_fetch_row.
Obtain each row of result_set as an associated array or/and a value index array.
Two types of arrays are obtained by default. you can set result_type:
MYSQL_ASSOC: returns the associated array, field name => field value
MYSQL_NUM: returns a numeric index array.
MYSQL_BOTH: obtains two types of arrays. Therefore, each field can be referenced by index offset or field name.
Example:
- $ Query = "select id,
Name from product order by name ";
- $ Result = mysql_query ($ query );
- While ($ row = mysql_fetch_array
($ Result, MYSQL_BOTH )){
- $ Name = $ row ['name'];
- // Or $ name = $ row [1];
- $ Name = $ row ['id'];
- // Or $ name = $ row [0];
- Echo "Product: $ name ($ id )";
- }
Mysql_fetch_assoc ()
Array mysql_fetch_assoc (resource result_set)
Equivalent to mysql_fetch_array ($ result, MYSQL_ASSOC)
PHP mysql_fetch_object ()
Object mysql_fetch_object (resource result_set)
Like mysql_fetch_array (), the returned object is not an array.
Example:
- $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)";
- }
These functions are all summary of the function used by PHP to obtain and display database data.
Mysql_result () mixed mysql_result (resource result_set, int row [, mixed field]) for retrieving the number of fields from the specified row of result_set in using PHP...