PHP Tutorial
PHP Flow Control
PHP Functions
PHP String
PHP Array
PHP Date Time
PHP Object Oriented
Regular Expression
PHP Cookie & Session
PHP Error & Exception handling
MySQL in PHP
PHP File Directory
PHP Image Processing
The mysqli_num_rows()
function in PHP is used to return the number of rows in a result set. This function is very useful when you want to know how many rows are returned in a SELECT query, or how many rows are affected by a DELETE, INSERT, REPLACE, or UPDATE query.
Here's a simple tutorial on how to use the mysqli_num_rows()
function.
Firstly, you'll need to establish a connection to your MySQL database using mysqli_connect()
.
// Database credentials $db_host = 'localhost'; $db_user = 'username'; $db_pass = 'password'; $db_name = 'database_name'; // Create connection $conn = mysqli_connect($db_host, $db_user, $db_pass, $db_name); // Check connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); }
Next, you can use a SELECT query to get data from your database:
// SQL query $sql = "SELECT * FROM table_name"; // Execute query and get result $result = mysqli_query($conn, $sql); if ($result) { // Use mysqli_num_rows() to get the number of rows $num_rows = mysqli_num_rows($result); echo "Number of rows: " . $num_rows; } else { echo "Error: " . mysqli_error($conn); }
In this example, mysqli_query()
executes the SQL query and returns a result set. Then, mysqli_num_rows()
is used to get the number of rows in that result set.
Lastly, don't forget to close the connection when you're done:
// Close connection mysqli_close($conn);
Important: The mysqli_num_rows()
function will only return the correct number of rows on SELECT statements. For DELETE, INSERT, REPLACE, or UPDATE statements, use mysqli_affected_rows()
instead.
Counting rows in MySQL result set with mysqli_num_rows():
mysqli_num_rows()
function is used to get the number of rows in a result set.<?php $mysqli = new mysqli('localhost', 'username', 'password', 'database'); // Check connection if ($mysqli->connect_error) { die('Connection failed: ' . $mysqli->connect_error); } // Execute a SELECT query $result = $mysqli->query('SELECT * FROM users'); // Get the number of rows $rowCount = mysqli_num_rows($result); echo "Number of Rows: $rowCount"; // Close the connection $mysqli->close();
How to use mysqli_num_rows() in PHP:
mysqli_num_rows()
on the result set obtained from a SELECT query.<?php $result = $mysqli->query('SELECT * FROM products'); $rowCount = mysqli_num_rows($result); echo "Number of Rows: $rowCount";
Checking if a MySQL query returned any rows in PHP:
mysqli_num_rows()
to check if any rows were returned.<?php $result = $mysqli->query('SELECT * FROM orders'); $rowCount = mysqli_num_rows($result); if ($rowCount > 0) { echo "Rows found!"; } else { echo "No rows found!"; }
Examples of mysqli_num_rows() function in PHP:
mysqli_num_rows()
in different scenarios.<?php $result1 = $mysqli->query('SELECT * FROM customers'); $rowCount1 = mysqli_num_rows($result1); $result2 = $mysqli->query('SELECT * FROM products WHERE price > 50'); $rowCount2 = mysqli_num_rows($result2); echo "Rows in Customers Table: $rowCount1, Rows with Price > 50: $rowCount2";
Handling conditional statements with mysqli_num_rows():
mysqli_num_rows()
in conditional statements.<?php $result = $mysqli->query('SELECT * FROM messages WHERE status = "unread"'); $rowCount = mysqli_num_rows($result); if ($rowCount > 0) { echo "You have unread messages!"; } else { echo "No unread messages."; }
Retrieving the number of rows in a SELECT query in PHP:
mysqli_num_rows()
to count rows in a SELECT query.<?php $result = $mysqli->query('SELECT * FROM books'); $rowCount = mysqli_num_rows($result); echo "Number of Books: $rowCount";
Using mysqli_num_rows() for result set validation in PHP:
mysqli_num_rows()
before processing.<?php $result = $mysqli->query('SELECT * FROM employees WHERE department = "IT"'); if ($result && mysqli_num_rows($result) > 0) { // Process the result set } else { echo "No employees found in the IT department."; }
Common issues and troubleshooting with mysqli_num_rows():
mysqli_num_rows()
.<?php $result = $mysqli->query('SELECT * FROM products'); if ($result === false) { die('Query failed: ' . $mysqli->error); } $rowCount = mysqli_num_rows($result); echo "Number of Rows: $rowCount";
Alternative methods to get the number of rows in PHP MySQLi:
COUNT(*)
or fetching all rows and counting in PHP.<?php // Using COUNT(*) $result = $mysqli->query('SELECT COUNT(*) FROM users'); $rowCount = $result->fetch_row()[0]; // Fetching all rows and counting in PHP $result = $mysqli->query('SELECT * FROM orders'); $rowCount = $result->num_rows; echo "Number of Rows: $rowCount";