The difference _php skill of MYSQL_FETCH_ROW,MYSQL_FETCH_ARRAY,MYSQL_FETCH_ASSOC

Source: Internet
Author: User
Copy Code code as follows:

<?php
$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]. ' <br> ';
}
$result = mysql_query ($sql);
while ($row = Mysql_fetch_array ($result))
{
echo $row [' CID ']. ':: '. $row [1]. ' <br> ';
}
$result = mysql_query ($sql);
while ($row = Mysql_fetch_object ($result))
{
echo $row->cid. ':: '. $row->title. " <br> ";
}
$result = mysql_query ($sql);
while ($row = Mysql_fetch_assoc ($result))
{
echo $row [' CID ']. ':: '. $row [1]. ' <br> ';
}
?>

Analysis:
Mysql_fetch_row, this function takes one row from the result set as the enumeration data, obtains a row of data from the result set associated with the specified result identity, and returns as an array. The columns of each result are stored in a cell of an array, with an offset starting at 0. Note that this is offset from 0, which means that you cannot use the field name to take the value, only the index to use the value, so the following code is not a value:
while ($row = Mysql_fetch_row ($res)) {
echo $row [' CID ']. ':: '. $row [1]. ";
}//The $row[' CID ' in here cannot get the value.
Mysql_fetch_array, take a row from the result set as an associative array, or an array of numbers, or both, in addition to storing the data in an array as a digital index, you can store the data as an associated index, using the field name as the key name. Which means he gets the same result as an array, can use key or index to get the value, so
while ($row = Mysql_fetch_array ($res)) {
echo $row [' CID ']. ':: '. $row [1]. ";
}//here $row[' CID ', $row [1] can get the corresponding value.
Mysql_fetch_object, as the name suggests, takes a row from the result set as an object and the field name as an attribute. So that's the only way to get a value.
while ($row = Mysql_fetch_object ($res)) {
echo $row->cid. ':: '. $row->title. "";
}
MYSQL_FETCH_ASSOC, take a row from the result set as an associative array, which means that the function cannot be indexed as Mysql_fetch_row, only with the name of the field, so
while ($row = Mysql_fetch_assoc ($res)) {
echo $row [' CID ']. ':: '. $row [1]. ";
}//$row [1] This is not a value
Add one point:
The Mysql_fetch_array function is defined as this: array mysql_fetch_array (resource result [, int result_type]), returns an array generated from rows obtained from the result set, and returns if there are no more rows FALSE.
The optional second parameter in mysql_fetch_array () Result_type is a constant that can accept the following values: Mysql_assoc,mysql_num and Mysql_both. which
1, Mysql_fetch_assoc ($result) ==mysql_fetch_array ($result, MYSQL_ASSOC);
2, Mysql_fetch_row ($result) ==mysql_fetch_array ($result, mysql_num);
So the mysql_fetch_array () function can in some way be considered a collection of mysql_fetch_row () and Mysql_fetch_assoc (). In addition, Mysql_fetch_array () also has the Mysql_both parameter, which will get an array that contains both an association and a numeric index.
In the sentence $row = $db->fetch_array ($query);
$DB is the human database operation class, $db->fetch_array ($query), Fetch_array ($query) is the method in that DB class, $row = $db->fetch_array ($query) The meaning of this sentence is to get a row of records in the database from the recordset $query.
Do not use the class can be implemented
Copy Code code 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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.