Home>Article>Backend Development> Commonly used functions for obtaining SQL query results in PHP (detailed examples)
In the previous article, I brought you "Usage of mysqli_select_db and mysqli_query functions in PHP", which gave you a detailed introduction to how to use the two functions and their main functions. In this article, we continue to look at how to obtain SQL query results in PHP. I hope everyone has to help!
In the previous article, we talked about how to execute a SQL statement, which is to call themysqli_query()
function. Through this function we can already The database information has been queried, but in our daily development we still need to process the result to get the information we want. Next, let’s take a look at several functions commonly used to process results in PHP.
mysqli_fetch_row()
function
mysqli_fetch_row() function You can get a row from the result set and return it in the form of an index array. The syntax format is as follows:
mysqli_result::fetch_row()
This is object-oriented writing, and the process-oriented writing is as follows:
mysqli_fetch_row(mysqli_result $result)
You need to pay attention to it are:mysqli_result
and$result
are represented as the result set obtained using the mysqli_query() function.
Next let’s take a look at the usage of the mysqli_fetch_row() function through an example. The example is as follows:
connect_errno){ die('数据库连接失败:'.$mysql->connect_errno); }else{ $sql = 'select name,sex,age from user'; // SQL 语句 $result = $mysql -> query($sql); // 执行上面的 SQL 语句 $data = $result -> fetch_row(); $mysql -> close(); } echo ''; print_r($data); ?>Output result:
In the above example, one row of data in the database is successfully queried through the mysqli_fetch_row() function, and is returned in the form of an index array. Then let's take a look at the different return forms.
mysqli_fetch_assoc()
functionmysqli_fetch_assoc() function You can get a row from the result set and return it in the form of an associative array. The syntax format of this function is as follows:
mysqli_result::fetch_assoc()This is its object-oriented syntax format, and the following is its procedural-oriented syntax format:
mysqli_fetch_assoc(mysqli_result $result)It should be noted that:
mysqli_result
and$result
are represented as result sets obtained using the mysqli_query() function.Next let’s look at the use of the mysqli_fetch_assoc() function through an example. The example is as follows:
'; print_r($data); ?>Output result:
From the above example, we successfully obtained a row of information in the database through the mysqli_fetch_assoc() function and returned it through an associative array. We can also control the form of the returned data through a function, so that it can be an index array, an associative array, or a combination of both. In this case, we will use the mysqli_fetch_array() function.
mysqli_fetch_array()
functionmysqli_fetch_array() function You can get a row from the result set and return it in the form of an associative array, an index array, or both according to the parameters. Its syntax format is as follows:
mysqli_result::fetch_array([int $resulttype = MYSQLI_BOTH])This is an object-oriented syntax, and the following is process-oriented The syntax:
mysqli_fetch_array(mysqli_result $result[, int $resulttype = MYSQLI_BOTH])It should be noted that:
##mysqli_resultand
$resultare expressed as The result set obtained using the mysqli_query() function.
$resulttypeis an optional parameter. It is a constant used to set the type of return value. Its value can be
MYSQLI_ASSOC,
MYSQLI_NUMor
MYSQLI_BOTHindicates different types of return values.
'; print_r($data); ?>Output result:
mysqli_fetch_all()
Function
mysqli_result::fetch_all([int $resulttype = MYSQLI_NUM])This is object-oriented writing, and the following is process-oriented The writing method:
mysqli_fetch_all(mysqli_result $result [, int $resulttype = MYSQLI_NUM])It should be noted that: the syntax is the same as the mysqli_fetch_array() function
t and$result
Represented as a result set obtained using the mysqli_query() function.
$resulttype
为可选参数,它是一个常量,用来设定返回值的类型,它的取值可以是MYSQLI_ASSOC
、MYSQLI_NUM
或MYSQLI_BOTH
表示返回值的不同类型。
接下来通过示例来看一下mysqli_fetch_all() 函数的使用,示例如下:
connect_errno){ die('数据库连接失败:'.$mysql->connect_errno); }else{ $sql = 'select name,sex,age from user'; // SQL 语句 $result = $mysql -> query($sql); // 执行上面的 SQL 语句 $data = $result -> fetch_all(MYSQLI_ASSOC); $mysql -> close(); } echo ''; print_r($data); ?>输出结果:
上述示例中,便是通过mysqli_fetch_all() 函数选择以关联数组的形式返回所有的数据。
大家如果感兴趣的话,可以点击《PHP视频教程》进行更多关于PHP知识的学习。
The above is the detailed content of Commonly used functions for obtaining SQL query results in PHP (detailed examples). For more information, please follow other related articles on the PHP Chinese website!