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.
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!