Differences between phpmysql_fetch_array and mysql_fetch_assoc

Source: Internet
Author: User
The difference between the two functions has always been vague. I only know that mysql_fetch_assooc () can remove duplicate values in the result set. in actual applications, mysql_fetch_assoc () is generally used (), today I thought of the two functions, and I checked their differences.

The difference between the two functions has always been vague. I only know that mysql_fetch_assooc () can remove duplicate values in the result set. in actual applications, mysql_fetch_assoc () is generally used (), after thinking about the two functions, I checked their differences and learned more about 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 extended version 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 priority and you need to access other columns with the same name, you must use the numeric index of this column or create 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 a number array, or both.

Note:Mysql_fetch_assoc () is exactly the same as mysql_fetch_array () and the Second Optional parameter MYSQL_ASSOC. it only returns the joined array. this is also the initial method of mysql_fetch_array (), that is, mysql_fetch_array, 'MySQL _ ASSOC ') and mysql_fetch_assoc ($ result) return results are the same.

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.

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.

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:

  1. Array ([username] => 'test', [password] => '000000 ')
  2. Array ([0] => 'test', [1] => '123 ')
  3. 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.