count number of rows in mysql query result in php code example
Example 1: php num rows
<?php
$con = mysqli_connect("localhost", "root", "", "testing");
if (mysqli_connect_errno()) {
echo "Database connection failed.";
}
$query = "SELECT Username, Password FROM users";
$result = mysqli_query($con, $query);
if ($result) {
$row = mysqli_num_rows($result);
if ($row) {
printf("Number of row in the table : " . $row);
}
mysqli_free_result($result);
}
?>
Example 2: php 7 count result in database
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$link = mysqli_connect("host", "username", "password","db_name");
mysqli_set_charset($link, "utf8mb4");
$result = mysqli_query($link, "SELECT count(*) FROM blackandwhite");
$num_rows = mysqli_fetch_row($result)[0];
echo "$num_rows Rows\n";