For a long time, many beginners have no idea about the differences between the functions used to retrieve data from the query result set in Mysql. today I will show you the difference. I will give you a PHP instance before the show.
The code is as follows:
$ Link = mysql_connect ('localhost', 'root ',");
Mysql_select_db ('ABC', $ link );
$ SQL = "select * from book ";
$ Result = mysql_query ($ SQL );
While ($ row = mysql_fetch_row ($ result ))
{
Echo $ row ['CID']. ':'. $ row [1].'
';
}
$ Result = mysql_query ($ SQL );
While ($ row = mysql_fetch_array ($ result ))
{
Echo $ row ['CID']. ':'. $ row [1].'
';
}
$ Result = mysql_query ($ SQL );
While ($ row = mysql_fetch_object ($ result ))
{
Echo $ row-> cid. ':'. $ row-> title ."
";
}
$ Result = mysql_query ($ SQL );
While ($ row = mysql_fetch_assoc ($ result ))
{
Echo $ row ['CID']. ':'. $ row [1].'
';
}
?>
Analysis:
Mysql_fetch_row: This function extracts a row from the result set as Enumeration Data and obtains a row of data from the result set associated with the specified result ID and returns it as an array. Each result column is stored in an array unit, and the offset starts from 0. Note: Here the offset starts from 0. that is to say, the field name cannot be used for the value, but the index can only be used for the value. Therefore, the following code cannot obtain the value:
While ($ row = mysql_fetch_row ($ res )){
Echo $ row ['CID']. ':'. $ row [1]. ";
} // $ Row ['CID'] here cannot be set.
Mysql_fetch_array, get a row from the result set as an associated array, or a number array, or both, in addition to storing data in the array as a digital index, you can also store data as associated indexes and use field names as key names. That is to say, the result obtained by him is like an array and can be determined by key or index.
While ($ row = mysql_fetch_array ($ res )){
Echo $ row ['CID']. ':'. $ row [1]. ";
} // Here, $ row ['CID'] and $ row [1] can get corresponding values.
Mysql_fetch_object, as its name implies, retrieves a row from the result set as an object and uses the field name as an attribute. So only in this way can we get the value
While ($ row = mysql_fetch_object ($ res )){
Echo $ row-> cid. ':'. $ row-> title ."";
}
Mysql_fetch_assoc: obtains a row from the result set as an associated array. that is to say, this function cannot use indexes as mysql_fetch_row. Therefore, it can only use field names.
While ($ row = mysql_fetch_assoc ($ res )){
Echo $ row ['CID']. ':'. $ row [1]. ";
} // $ Row [1] cannot be obtained.
Add:
The mysql_fetch_array function is defined as follows: array mysql_fetch_array (resource result [, int result_type]) returns an array generated based on the rows obtained from the result set. If no more rows exist, FALSE is returned.
The second optional parameter result_type in mysql_fetch_array () is a constant and can accept the following values: MYSQL_ASSOC, MYSQL_NUM, and MYSQL_BOTH. Where:
1. mysql_fetch_assoc ($ result) = mysql_fetch_array ($ result, MYSQL_ASSOC );
2. mysql_fetch_row ($ result) = mysql_fetch_array ($ result, MYSQL_NUM );
Therefore, the mysql_fetch_array () function can be regarded as a collection of mysql_fetch_row () and mysql_fetch_assoc () to some extent. In addition, mysql_fetch_array () also has the MYSQL_BOTH parameter, and an array containing both the correlation and digital indexes will be obtained.
$ Row = $ db-> fetch_array ($ query );
$ Db is a human database operation class, $ db-> fetch_array ($ query), fetch_array ($ query) is the method in that db Class, $ row = $ db-> fetch_array ($ query) indicates that a row of records in the database is obtained from the record set $ query.
Class is not needed.
The code is as follows:
$ Conn = @ mysql_connect ($ host, $ user, $ pass );
@ Mysql_select_db ($ database, $ conn );
$ Query = mysql_query ($ SQL );
While ($ row = mysql_fetch_array ($ query )){
$ Rows [] = $ row;
}