mysql_fetch_* Column function
The main function of the mysql_fetch_* column function is to obtain the relevant query results from the result set returned by the query, mainly including:
- Mysql_fetch_array (): Gets a row from the result set as an associative array or an array of indices, or both
- Mysql_fetch_row (): Get one row from the result set as an enumeration array
- MYSQL_FETCH_ASSOC (): Get one row from the result set as an associative array
- Mysql_fetch_object (): Get one row from the result set as an object
- Mysql_fetch_field (): Gets field information from the result set and returns as an object
- Mysql_fetch_lengths (): Gets the length of the output of each field in the result set
- Mysql_fetch_array ()
The Mysql_fetch_array () function is used to get one row from the result set as an associative or indexed array, or both. Returns an array successfully, otherwise it returns FALSE.
Grammar:
Array mysql_fetch_array (resource result [, int result_type])
Parameter description:
- Result: A dataset resource returned by a query function (such as mysql_query)
- Result_type: An optional constant indicating the type of the array result, with acceptable values as follows:
- Mysql_both: By default, gets an array that contains both an association and a numeric index, using the field name as the key name
- MYSQL_ASSOC: An array that only gets the associated index
- Mysql_num: An array that only gets a numeric index
Example 1, using Mysql_num:
<?php
$conn = @mysql_connect ("localhost", "root", "root123");
if (! $conn) {
die ("Connection Database failed:". Mysql_error ());
}
mysql_select_db ("Test", $conn);
mysql_query ("Set character set ' GBK '");
$result = mysql_query ("SELECT uid,username from User");
while ($row = Mysql_fetch_array ($result, Mysql_num)) {
echo User id:. $row [0]. " <br/> ";
echo "Username:". $row [1]. " <br/> ";
}
? >
Browser output:
User id:1
User name: admin
user id:2 username:
xiaoming
user id:3 username:
Jack
user id:4
user name: Xiao Wang
Example 2, using Mysql_ ASSOC:
Duplicate code omitted
$result = mysql_query ("SELECT uid,username from User");
while ($row = Mysql_fetch_array ($result, Mysql_ ASSOC)) {
echo User id:. $row [' uid ']. " <br/> ";
echo "Username:". $row [' username ']. " <br/> ";
}
The browser output is ditto.
When you use Mysql_both or omit this parameter, you will also have mysql_num and mysql_ ASSOC characteristics.
Description
The field name returned by this function is case-sensitive for the array key value
Using Mysql_fetch_array () is not significantly slower than using mysql_fetch_row (), but also provides significantly more value
The function returns only one row of data from the current data pointer and, if executed once, points the data pointer to the next column of data
If you want to get multiple rows or all of the data, you need to use a looping structure to remove the data line by row
If two or more columns in the result have the same field name, the last column takes precedence. To access other columns of the same name, you must use the numeric index of the column or give the column an alias
mysql_fetch_row ()
PHP's MySQL action function mysql_fetch_row () is used to get one row from the result set as an enumeration array. Returns an array successfully, otherwise it returns FALSE.
Grammar:
Array mysql_fetch_row (resource result)
This function behaves in accordance with mysql_fetch_array (resource result, mysql_num), please refer to the mysql_fetch_array () function usage, not to repeat here.
Mysql_fetch_object ()
PHP Operations MySQL function mysql_fetch_object () is used to obtain a row from the result set as an object, successfully return an object, otherwise return FALSE.
Grammar:
Object Mysql_fetch_object (Resource result)
Example:
<?php
$conn = @mysql_connect ("localhost", "root", "root123");
if (! $conn) {
die ("Connection Database failed:". Mysql_error ());
}
mysql_select_db ("Test", $conn);
mysql_query ("Set character set ' GBK '");
$result = mysql_query ("SELECT uid,username from User");
while ($row = Mysql_fetch_object ($result)) {
echo User id:. $row->uid. " <br/> ";
echo "Username:". $row->username. " <br/> ";
}
? >
Browser output:
User id:1
User name: admin
user id:2 username:
xiaoming
user id:3 username:
Jack
user id:4
user name: Xiao Wang