What is the difference between mysql_fetch_array (), mysql_fetch_row (), mysql_fetch_object (), and mysql_result () [discussion ???

Source: Internet
Author: User
Tags field table
What are their differences ??
What is the difference ??
What form of results are they stored and obtained ??
______________________________________________________________
Answer 3:
They are basically the same, but the returned array structure is different. For example
Select name, age, gender from users where age> 18
$ ROW = mysql_fetch_array () to obtain $ row can be accessed using 0, 1, 2, 'name', 'age'
That is, $ row [0] = $ row [name] = Name, $ row [1] = $ row [age] = Age
Mysql_result () is the same as fetch_array () and supports $ row ['user. age'], $ row ['user. name'],
It is convenient to select multiple tables.
Mysql_fetch_row () can only be 0, 1, 2, not 'name', 'age'
Use $ row-> name, $ row-> Age, $ row-> gender for mysql_fetch_object ()
______________________________________________________________
Answer 4:
Mysql_fetch_array --- obtain the queried array result
Syntax: array mysql_fetch_array (INT result [, int result_type])
Note:
Returns an array. This array is equivalent to obtaining columns. If no more columns exist, false is returned.
Mysql_fetch_array () is an extension of mysql_fetch_row (). In addition to storing data in the numerical index of the array, mysql_fetch_array () can also store data in the composite index, use the column name as the index value.
If more than two columns in the query result have the same name, the last column is valid. To access columns with the same name, you must take an alias for the column.
Select t1.f1 as Foo t2.f1 as bar from T1, T2
The second parameter result_type of mysql_fetch_array () is required. result_type is a constant and has the following values: mysql_assoc, mysql_num, and mysql_both (New in php3.0.7)
Mysql_fetch_row --- obtain the result of a Single Column
Syntax: array mysql_fetch_row (INT result)
Note:
Returns an array. This array is equivalent to obtaining columns. If no more columns exist, false is returned.
Mysql_fetch_row () is a column that retrieves data from the result and puts the column into the array for transmission. Each result column is stored in the array offset. The starting value of the Offset is 0.
Call mysql_fetch_row () to return the next column in the result. If no more columns exist, return false.
Mysql_fetch_object --- obtain the object result after Query
Syntax: Object mysql_fetch_object (INT result [, int result_typ])
Note:
Returns an object. This object is equivalent to obtaining a column. If no more columns exist, Fales is returned.
Mysql_fetch_object () is similar to mysql_fetch_array (). The difference is that the function returns an object instead of an array, which indirectly means that you can access data by column name, and it is not based on their offset (offset) (numbers are invalid attribute names)
The optional result_type is a constant and can be the following values: mysql_assoc, mysql_num, and mysql_both.
This function is the same as mysql_fetch_array () and has the same speed as mysql_fetch_row.

Mysql_result --- obtain results
Syntax: int mysql_result (INT result, int row [, mixed field])
Note:
Mysql_result () returns the content of a cell from the MySQL result. The parameter field can be the offset of the column, the column name, or fieldname. tablename. If the column name is an alias ('select Foo as bar from... '), the alias is used to replace the column name.
When working on large results, you should consider getting the entire column. These functions return a variety of cell content, which will make the table mysql_result () faster. Similarly, the offset specified in the field parameter is faster than specifying a column name or tablename. fieldname.
Calling mysql_result () should not be mixed with calling other functions.
______________________________________________________________
Answer 5:
Upstairs
I have tried mysql_fetch_row () and can use 'name', 'age'
______________________________________________________________
Answer 6:
Wrong. It's upstairs.
______________________________________________________________
Answer 7:
I knew it when I used it.
______________________________________________________________
Answer 8:
Mysql_fetch_array () returns an array. The default value is 2 * Number of fields. One of the following labels and one group are named. Set the constant mysql_assoc, mysql_num, and mysql_both.
Mysql_fetch_row () returns only the subscript Array
Mysql_fetch_object () returns an object. A name is used as an attribute.
Mysql_result () gets an element. The query result is regarded as a two-dimensional table, and the data of column Y (field) in X rows (Records) is returned.
Do not use mysql_result () unless otherwise required ()
The other three are equally efficient and can be selected based on your needs and preferences.
______________________________________________________________
Answer 9:
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 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 ').

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 () retrieves a row of data from the result set associated with the specified result ID and returns it as an array. Each result column is stored in an array unit, and the offset starts from 0.
Call mysql_fetch_row () in sequence to return the next row in the result set. If no more rows exist, false is returned.

Object mysql_fetch_object (resource result)
Returns the object generated based on the row obtained. If no more rows exist, false is returned.
Mysql_fetch_object () is similar to mysql_fetch_array (). There is only one difference-an object instead of an array is returned. Indirectly, it means that you can only access the array by field name, rather than the offset (the number is a valid attribute name ).
Note: The field names returned by this function are case sensitive.

Mixed mysql_result (resource result, int row [, mixed field])
Mysql_result () returns the content of a unit in the MySQL result set. The field parameter can be the offset or field name of the field, or tablename. fieldname of the field table ). If an alias ('select Foo as bar from... ') is given to the column, the column name is replaced by an alias.
When used as a large result set, you should consider using a function that can obtain the entire row (as shown below ). These functions return the content of multiple units in the callback function call, which is much faster than mysql_result. In addition, it is much faster to specify the offset in the field parameter than to specify the field name or tablename. fieldname.
Calling mysql_result () cannot be used together with other functions that process result sets.

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.