How to get query results in php

coldplay.xixi
Release: 2023-03-04 15:30:02
Original
3626 people have browsed it

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);
Copy after login

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);
Copy after login

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;
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template