mysql num_rows 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";
Example 3: mysql num rows
<?php
$link = mysql_connect("localhost", "mysql_user", "mysql_password");
mysql_select_db("database", $link);
$result = mysql_query("SELECT * FROM table1", $link);
$num_rows = mysql_num_rows($result);
echo "$num_rows Rows\n";
?>
Example 4: mysql row_number() example
SET @row_number = 0;
SELECT
(@row_number:=@row_number + 1) AS num,
firstName,
lastName
FROM
employees
ORDER BY firstName, lastName
LIMIT 5;