Home  >  Article  >  Backend Development  >  How to return array in php sql query

How to return array in php sql query

藏色散人
藏色散人Original
2020-10-16 10:12:073825browse

php SQL query returns the array method: 1. Use the mysql_fetch_row function; 2. Through the mysql_fetch_assoc function; 3. Use the mysql_fetch_array function; 4. Use the mysql_fetch_object function.

How to return array in php sql query

Recommended: "PHP Video Tutorial"

Several ways to convert mysql query results into PHP arrays The difference between methods

$result = mysql_fetch_row(): This function returns an array. The array is subscripted with numbers. You can only pass $result[0],$Result[2] like this citation form.

$result = mysql_fetch_assoc(): This function returns an array subscripted by the field name, which can only be referenced by the field name.

$result['field1']. $result = mysql_fetch_array(): This function returns a mixed array that can be referenced either by numeric subscript or by field name.

$result[0] or $result["field1"]. $result = mysql_fetch_object(): Returns the result in the form of an object, which can be referenced in the form $result->field1.

It is recommended to use mysql_fetch_assoc() or mysql_fetch_array. These two functions execute faster and can also be referenced through field names, which is clearer.

The above is the detailed content of How to return array in php sql query. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn