The browser output is as follows.
When you use Mysql_both or omit this argument, you will have both Mysql_num and mysql_ ASSOC attributes.
MySQL the Mysql_fetch_array function obtains a row in the query result as an array
mysql_fetch_* column Functions
The main function of the mysql_fetch_* column function is to obtain related 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 indexed array, or both
- Mysql_fetch_row (): Gets a row from the result set as an enumeration array
- MYSQL_FETCH_ASSOC (): Gets a row from the result set as an associative array
- Mysql_fetch_object (): Gets a 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 each field content output in the result set
mysql_fetch_array ( )
The Mysql_fetch_array () function is used to get a row from the result set as an associative array or an indexed array, or both. Returns an array successfully, otherwise returns FALSE.
Grammar:
parameter Description:
Parameters |
Description |
Result |
A dataset resource returned by a query function (such as mysql_query) |
Result_type |
An optional constant indicating the array result type, acceptable values are as follows:
- Mysql_both: By default, you get an array that contains both associative and numeric indexes, with the field name as the key name
- MYSQL_ASSOC: Get an array of associated indexes only
- 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 "User name:". $row [1]. " <br/> "; }?>
Browser output:
User Id:1
User name: admin
User Id:2
User name: Xiao Ming
User Id:3
User name: 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 as follows.
When you use Mysql_both or omit this argument, you will have both Mysql_num and mysql_ ASSOC attributes.
Description
- The field masterpiece returned by this function is case-sensitive for an array key value
- Using Mysql_fetch_array () is not significantly slower than using mysql_fetch_row (), and also provides significantly more values
- This function only takes a row of data from the current data pointer and returns it as a result, 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 take the data out line by row
- If two or more columns in the result have the same field name, the last column takes precedence. To access another column of the same name, you must either use the column's numeric index or give the column an alias
- mysql_fetch_row ()
PHP's MySQL operation function mysql_fetch_row () is used to get a row from the result set as an enumerated array. Returns an array successfully, otherwise returns FALSE.
Grammar:
This function behaves in accordance with mysql_fetch_array (resource result, mysql_num), please refer to the mysql_fetch_array () function usage, not in this context.
mysql_fetch_object ()PHP Operation MySQL function Mysql_fetch_object () is used to get a row from the result set as an object, return an object successfully, otherwise return FALSE.
Grammar:
Example:
<?php $conn = @mysql_connect ("localhost", "root", "root123"); if (! $conn) {die ("Connect 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/> ";
Browser output:
User Id:1 User name: Admin user id:2 user name: Xiao Ming user id:3 user name: Jack user id:4 user name: Xiao Wang