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

How to get database query results in php

王林
Release: 2023-03-05 20:42:02
Original
4301 people have browsed it

php method to obtain database query results: You can use the mysqli_fetch_array() function to obtain it. The mysqli_fetch_array() function can obtain a row from the database query result set as an array, such as [mysql_query($sql)].

How to get database query results in php

mysqli_fetch_array() function fetches a row from the result set as an associative array, a numeric array, or both.

(Recommended tutorial: php video tutorial)

First connect, then execute the SQL statement to obtain the result set of the data. PHP has multiple functions that can obtain the result set of data. Mysql_fetch_array is most commonly used. By setting parameters, the subscript of the row data, the subscript of the numeric index and the subscript of the field name associated index are changed.

$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, you can 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 recommendations: php training

The above is the detailed content of How to get database 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