Differences between php mysql_fetch_array and mysql_fetch_assoc

Source: Internet
Author: User
Tags numeric mysql tutorial php tutorial

Php Tutorial mysql tutorial _ difference between fetch_array and mysql_fetch_assoc


The difference between the two functions has always been vague. Only knowing that mysql_fetch_assooc () can remove duplicate values in the result set, and mysql_fetch_assoc () is generally used in actual applications (). Today, I thought of the two functions, and I checked their differences to further understand them. The mysql_fetch_assoc () function retrieves a row from the result set as an associated array. Returns the associated 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,
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 this column.
Or an alias for this column. For a column with an alias, you cannot use the original column name to access its content ('field' in this example '). Mysql_fetch_array -- get a row from the result set as an associated array, or an array of numbers, or both. Note: mysql_fetch_assoc () is identical to mysql_fetch_array () and the Second Optional parameter MYSQL_ASSOC. It only returns the associated array. This is also the initial working method of mysql_fetch_array. That is, mysql_fetch_array ($ result, 'MySQL _ ASSOC ') returns the same result as mysql_fetch_assoc ($ result). Note: If you need a digital index other than the associated index, use mysql_fetch_array ().
Note: The field names returned by this function are case sensitive.


In mysql_fetch_assoc, the associated array is obtained.
Mysql_fetch_array can be used to obtain the associated array, index array, or both.

The following is an example to illustrate their differences:
Assume that a user's username and password are retrieved from the database tutorial.
Username password
Test 1, 123456
The assoc result is array ([username] => 'test', [password] => '123 ')
Array may be one of the following three results based on different parameters:
Array ([username] => 'test', [password] => '000000 ')
Array ([0] => 'test', [1] => '123 ')
Array ([username] => 'test', [password] => '000000', [0] => 'test', [1] => '000000 ')

 

Tips and comments
Note: mysql_fetch_assoc () is identical to mysql_fetch_array () and the Second Optional parameter MYSQL_ASSOC. It only returns the associated array. This is also the initial working method of mysql_fetch_array.

Tip: If you need a numeric index in addition to the associated index, use mysql_fetch_array ().

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

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.