How can I count the numbers of rows that a MySQL query returned?
You can use the mysqli_num_rows()
function to count the number of rows that a MySQL query returned in PHP.
Here's an example of how to use mysqli_num_rows()
:
<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "my_db");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT * FROM my_table WHERE column_name = 'some value'";
$result = mysqli_query($link, $query);
$num_rows = mysqli_num_rows($result);
printf("Result set has %d rows.\n", $num_rows);
/* close connection */
mysqli_close($link);
Watch a video course
Learn object oriented PHP
This code connects to a MySQL database, executes a SELECT query, and then uses mysqli_num_rows()
to count the number of rows returned by the query. It then prints out the number of rows.