Differences between mysql_fetch_assoc and mysql_fetch_array and mysql_fetch_row

Source: Internet
Author: User
This function extracts a row from the result set as Enumeration data, and obtains a row of data from the result set associated with the specified result ID and returns the data as an array. Each result column is stored in an array unit.

Mysql_fetch_rowThis function retrieves a row from the result set as Enumeration data, and obtains a row of data from the result set associated with the specified result ID and returns the row as an array. Each result column is stored in an array unit, and the offset starts from 0. Note: Here the offset starts from 0. that is to say, the field name cannot be used for the value, but the index can only be used for the value. Therefore, the following code cannot obtain the value:

While ($ row = mysql_fetch_row ($ result )){

Echo $ row ['id']. ':'. $ row [1]. ";

} // $ Row ['id'] here cannot be taken.


Mysql_fetch_arrayTo obtain a row from the result set as an associated array, or number array, or both. in addition to storing data in the array as a digital index, you can also store the data as an associated index, use the field name as the key name. That is to say, the result obtained by him is like an array and can be determined by key or index.

While ($ row = mysql_fetch_array ($ result )){

Echo $ row ['id']. ':'. $ row [1]. ";

} // Here, $ row ['CID'] and $ row [1] can get corresponding values.


Mysql_fetch_object,As the name suggests, get a row from the result set as an object and use the field name as an attribute. So only in this way can we get the value

While ($ row = mysql_fetch_object ($ result )){

Echo $ row-> id. ':'. $ row-> title ."";

}

Mysql_fetch_assocTo obtain a row from the result set as an associated array. that is to say, this function cannot use indexes to take values like mysql_fetch_row. Therefore, it can only use field names.

While ($ row = mysql_fetch_assoc ($ result )){

Echo $ row ['id']. ':'. $ row [1]. ";

} // $ Row [1] cannot be obtained.

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.