How can a PHP program determine the number of rows in a query result?
时间: 2023-11-27 11:49:27 浏览: 79
To determine the number of rows in a query result in PHP, you can use the `mysqli_num_rows()` function. This function takes a result set object as its argument and returns the number of rows in the result set.
Here's an example:
```
// Connect to database
$conn = mysqli_connect("localhost", "username", "password", "database");
// Execute query
$result = mysqli_query($conn, "SELECT * FROM mytable");
// Get number of rows
$num_rows = mysqli_num_rows($result);
// Print number of rows
echo "Number of rows: " . $num_rows;
```
This code connects to a MySQL database, executes a SELECT query, and then uses `mysqli_num_rows()` to get the number of rows in the result set. The number of rows is then printed to the screen.
阅读全文