Use of mysql_fetch_row () and mysql_fetch_array ()

Source: Internet
Author: User

Mysql_fetch_array-- Get a row from the result set as an associated array or number array, or both

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

Returns the array generated based on the rows obtained from the result set. If no more rows exist, FALSE is returned.
Mysql_fetch_array () is an extension of mysql_fetch_row. In addition to storing data in an array as a digital index, you can also store data as an associated index and use 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 with the same name, you must use the numeric index of the column or create an alias for the column. For a column with an alias, you cannot use the original column name to access its content ('field' in this example ').
Example 1. query with the same field name
Copy codeThe Code is as follows:
Select table1.field as foo, table2.field as bar from table1, table2

It is important to note that mysql_fetch_array () is not much slower than mysql_fetch_row (), and more obvious values are provided.

Mysql_fetch_array ()The second optional parameter result_type is a constant and can accept the following values: MYSQL_ASSOC, MYSQL_NUM, and MYSQL_BOTH. This feature is newly added in PHP 3.0.7. The default value of this parameter is MYSQL_BOTH.

If MYSQL_BOTH is used, an array containing both join and numeric indexes is obtained. Use MYSQL_ASSOC to obtain only the associated index (like mysql_fetch_assoc (), and use MYSQL_NUM to obtain only the numeric index (like mysql_fetch_row ).

Note: The field names returned by this function are case sensitive.

Example 2. mysql_fetch_array use MYSQL_NUM
Copy codeThe Code is as follows:
<? Php
Mysql_connect ("localhost", "mysql_user", "mysql_password") or
Die ("cocould 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 use MYSQL_ASSOC
Copy codeThe Code is as follows:
<? Php
Mysql_connect ("localhost", "mysql_user", "mysql_password") or
Die ("cocould 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 use MYSQL_BOTH
Copy codeThe Code is as follows:
<? Php
Mysql_connect ("localhost", "mysql_user", "mysql_password") or
Die ("cocould 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 -- obtains a row from the result set as an enumerated Array

Description
Array mysql_fetch_row (resource result)
Returns the array generated based on the row obtained. If no more rows exist, FALSE is returned.
Mysql_fetch_row ()Obtain a row of data from the result set associated with the specified result ID and return it as an array. Each result column is stored in an array unit, and the offset starts from 0.
CallMysql_fetch_row ()Returns the next row in the returned result set. If no more rows exist, FALSE is returned.

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.