Home > Article > Backend Development > PHP database operation skills: How to use the mysqli_num_rows function to obtain the number of rows in the query result
PHP database operation skills: How to use the mysqli_num_rows function to obtain the number of rows in the query result
When performing PHP database operations, it is often necessary to obtain the number of rows in the query result. In order to facilitate processing, PHP provides the mysqli_num_rows function, which can quickly obtain the number of rows in the query result. This article will introduce how to use the mysqli_num_rows function to implement this function, with code examples.
Before using the mysqli_num_rows function, we first need to connect to the database. You can use the mysqli_connect function to connect to the database, as shown below:
$servername = "localhost"; $username = "root"; $password = "password"; $dbname = "mydatabase"; $conn = mysqli_connect($servername, $username, $password, $dbname); if (!$conn) { die("连接失败:" . mysqli_connect_error()); }
Next, we need to execute the query statement to obtain the data. You can use the mysqli_query function to execute the query statement and store the results in a variable, as shown below:
$sql = "SELECT * FROM users"; $result = mysqli_query($conn, $sql);
Here we executed a simple SELECT statement and queried all the data in the "users" table.
Once the query statement is executed, we can use the mysqli_num_rows function to get the number of rows in the result. This function accepts one parameter, which is the result set object of the query, and returns the number of rows in the result. An example is as follows:
$row_count = mysqli_num_rows($result); echo "查询结果的行数为:" . $row_count;
In the above example, we store the number of rows of the query result in the $row_count variable and print it out.
The following is a complete example showing how to use the mysqli_num_rows function to get the number of rows in the query result:
$servername = "localhost"; $username = "root"; $password = "password"; $dbname = "mydatabase"; $conn = mysqli_connect($servername, $username, $password, $dbname); if (!$conn) { die("连接失败:" . mysqli_connect_error()); } $sql = "SELECT * FROM users"; $result = mysqli_query($conn, $sql); $row_count = mysqli_num_rows($result); echo "查询结果的行数为:" . $row_count; mysqli_close($conn);
In the above In the code, we first connect to the database through the mysqli_connect function, then execute a SELECT statement and print out the number of rows in the query result. Finally, use the mysqli_close function to close the database connection.
Summary
This article introduces how to use the mysqli_num_rows function to get the number of rows in the query result, and provides a complete code example. By using this function, you can quickly and easily obtain the number of rows in the query result, which facilitates subsequent processing and analysis. I hope this article can be helpful to your PHP database operations!
The above is the detailed content of PHP database operation skills: How to use the mysqli_num_rows function to obtain the number of rows in the query result. For more information, please follow other related articles on the PHP Chinese website!