The mysqli_num_rows()
function in PHP is used to get the number of rows in a result set returned by a MySQL query. It is commonly used after executing a SELECT
query to determine how many rows are in the result set.
Syntax:
mysqli_num_rows($result);
- $result: The result set from a MySQL query (
mysqli_query()
), which must be a valid result of a SELECT query.
Return Value:
- It returns the number of rows in the result set as an integer.
- If no rows are found, it returns
0
. - If the query fails or the result is not a valid resource, it returns
FALSE
.
Example 1: Basic Usage of mysqli_num_rows
<?php
// Create a connection
$conn = mysqli_connect("localhost", "username", "password", "database");
// Check if the connection is successful
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Execute a query
$query = "SELECT * FROM users";
$result = mysqli_query($conn, $query);
// Get the number of rows
$num_rows = mysqli_num_rows($result);
// Output the number of rows
echo "Number of rows: " . $num_rows;
// Close the connection
mysqli_close($conn);
?>
In this example:
- We connect to the database and run a
SELECT * FROM users
query to retrieve all the rows from theusers
table. mysqli_num_rows($result)
returns the number of rows in the result set.- The result is printed using
echo
.
Example 2: Checking if Data Exists
You can also use mysqli_num_rows()
to check if the query returned any results:
<?php
// Create a connection
$conn = mysqli_connect("localhost", "username", "password", "database");
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Execute a query to find a user with a specific email
$email = 'example@example.com';
$query = "SELECT * FROM users WHERE email = '$email'";
$result = mysqli_query($conn, $query);
// Check if any row is returned
if (mysqli_num_rows($result) > 0) {
echo "User exists!";
} else {
echo "No user found with that email.";
}
// Close the connection
mysqli_close($conn);
?>
Here:
- We query the
users
table to check if there’s a user with the specified email. mysqli_num_rows($result)
checks if the result contains any rows. If it does, we know the user exists.
Example 3: Using mysqli_num_rows()
in a Loop to Display Data
<?php
// Create a connection
$conn = mysqli_connect("localhost", "username", "password", "database");
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Execute a query to get all users
$query = "SELECT * FROM users";
$result = mysqli_query($conn, $query);
// Check if there are any users
if (mysqli_num_rows($result) > 0) {
// Loop through the result and display data
while ($row = mysqli_fetch_assoc($result)) {
echo "Name: " . $row['name'] . "<br>";
echo "Email: " . $row['email'] . "<br><br>";
}
} else {
echo "No users found.";
}
// Close the connection
mysqli_close($conn);
?>
In this example:
- We check if the query returns any rows using
mysqli_num_rows()
. - If rows are returned, we loop through them using
mysqli_fetch_assoc()
to display each user's name and email.
Common Use Cases:
- To determine if a query returned any results.
- To validate the number of records found in a query.
- To control the flow of your program based on the number of records retrieved.
Notes:
mysqli_num_rows()
only works withSELECT
queries, not withINSERT
,UPDATE
, orDELETE
queries.- If the result is empty,
mysqli_num_rows()
will return 0.
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Copyright 2023-2025 © All rights reserved.