Introduction to the use of mysql_fetch_row () and mysql_fetch_array () _mysql

Source: Internet
Author: User

Mysql_fetch_array --take one row from the result set as an associative array, or an array of numbers, or both

Description array mysql_fetch_array (resource result [, int result_type])

Returns the array generated from the rows obtained from the result set, or FALSE if there are no more rows.
mysql_fetch_array () is an extended version of Mysql_fetch_row (). In addition to storing 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.

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 individual name. For an alias column, you cannot access its contents with the original column name (the ' field ' in this example).
Example 1. Query with same field name

Copy Code code as follows:

Select Table1.field as foo, Table2.field as bar from table1, table2

It is important to note that using mysql_fetch_array () is not significantly slower than using mysql_fetch_row () and provides significantly more value.

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. This feature is a new addition to PHP 3.0.7. The default value for this parameter is Mysql_both.

If you use Mysql_both, you get an array that contains both an association and a numeric index. Only the associated index (as MYSQL_FETCH_ASSOC ()) is obtained using MYSQL_ASSOC, and only the numeric index (as Mysql_fetch_row ()) is obtained with Mysql_num.

Note: The field name returned by this function is case sensitive.

Example 2. Mysql_fetch_array Use Mysql_num

Copy Code code as follows:

<?php
mysql_connect ("localhost", "Mysql_user", "Mysql_password") or
Die ("Could not connect:".) Mysql_error ());
mysql_select_db ("MyDB");

$result = mysql_query ("SELECT ID, name from mytable");

while ($row = Mysql_fetch_array ($result, mysql_num)) {
printf ("ID:%s Name:%s", $row [0], $row [1]);
}

Mysql_free_result ($result);
?>


Example 3 Mysql_fetch_array uses MYSQL_ASSOC
Copy Code code as follows:

<?php
mysql_connect ("localhost", "Mysql_user", "Mysql_password") or
Die ("Could not connect:".) Mysql_error ());
mysql_select_db ("MyDB");

$result = mysql_query ("SELECT ID, name from mytable");

while ($row = Mysql_fetch_array ($result, Mysql_assoc)) {
printf ("ID:%s Name:%s", $row ["id"], $row ["name"]);
}
Mysql_free_result ($result);
?>


Example 4 Mysql_fetch_array uses Mysql_both
Copy Code code as follows:

<?php
mysql_connect ("localhost", "Mysql_user", "Mysql_password") or
Die ("Could not connect:".) Mysql_error ());
mysql_select_db ("MyDB");

$result = mysql_query ("SELECT ID, name from mytable");

while ($row = Mysql_fetch_array ($result, Mysql_both)) {
printf ("ID:%s Name:%s", $row [0], $row ["name"]);
}
Mysql_free_result ($result);
?>


See Mysql_fetch_row () and MYSQL_FETCH_ASSOC
Mysql_fetch_row--get one row from the result set as an enumeration array

Description
Array mysql_fetch_row (resource result)
returns the array generated based on the obtained row, or FALSE if there are no more rows. The
mysql_fetch_row () obtains a row of data and returns as an array from the result set associated with the specified result identity. The columns of each result are stored in a cell of an array, with an offset starting at 0.
Call mysql_fetch_row () in turn returns the next row in the result set, or FALSE if there are no more rows.

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.