Home >Backend Development >PHP Problem >How to get query results in php

How to get query results in php

coldplay.xixi
coldplay.xixiOriginal
2020-08-17 10:00:383660browse

php method to obtain query results: 1. Connect and execute the SQL statement to obtain the result set of the data; 2. Obtain the numerical index array by setting the parameter [MYSQL_NUM]; 3. Traverse the entire Result set.

How to get query results in php

php method to obtain query results:

Connect first, then execute the SQL statement to obtain the data results set. PHP has multiple functions that can obtain the result set of data. mysql_fetch_array is most commonly used to change the subscript of row data, the subscript of numeric index and the subscript of field name-related index by setting parameters.

$sql = "select * from user limit 1";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);

You can get only the numeric index array by setting the parameter MYSQL_NUM, which is equivalent to the mysql_fetch_row function. If the parameter is set to MYSQL_ASSOC, only Get the associated index array, which is equivalent to the mysql_fetch_assoc function.

$row = mysql_fetch_row($result);
$row = mysql_fetch_array($result, MYSQL_NUM); //这两个方法获取的数据是一样的
$row = mysql_fetch_assoc($result);
$row = mysql_fetch_array($result, MYSQL_ASSOC);

If we want to get all the data in the data set, we loop through the entire result set.

$data = array();
while ($row = mysql_fetch_array($result)) {
    $data[] = $row;
}

Related learning recommendations: php programming (video)

The above is the detailed content of How to get query results in php. 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