How to Check If MySQL Results Returned Empty in PHP

How to check if a MySQL result is empty

You need to check with number of rows. if it return zero it means table contain no rows. try below code.

// Populate headers
$fields = $result->fetch_fields();
foreach ($fields as $field)
printf("<th>%s</th>", $field->name);


// Write to table
if($result->num_rows== 0){
echo "No Result to display";
while ($myvar = $result->fetch_row()) {

$date = $myvar[0];
$room_ID = $myvar[1];
$description = $myvar[2];
$firstname = $myvar[3];
$lastname = $myvar[4];
$message = $myvar[5];
$period = $myvar[6];

printf("<td>%s</td><td>%s</td>", $date, $room_ID);
printf("<td>%s</td><td>%s</td>", $description, $firstname);
printf("<td>%s</td><td>%s</td><td>%s</td>", $lastname, $message, $period);



How to check if MySQL returns null/empty?

Use empty() and/or is_null()

Empty alone will achieve your current usage, is_null would just make more control possible if you wanted to distinguish between a field that is null and a field that is empty.

How to check MySQL returned an empty result set?

Use rowCount

PDOStatement::rowCount — Returns the number of rows affected by the last SQL statement

if ($query1->rowCount() > 0) {
// not empty
} else {
echo 'nothing';

Since fetchAll returns an array, you can use count to get the number of rows returned from the query.

if (count($ajResult)) {...}

Best way to check if mysql_query returned any results?

if (mysql_num_rows($result)==0) { PERFORM ACTION }

For PHP 5 and 7 and above use mysqli:

if (mysqli_num_rows($result)==0) { PERFORM ACTION }

How do I check if a table is empty or already my query didn't match any results

I guess, that $conn is a PDO connection? In that case, the method $conn->query() returns an object of type PDOStatement. See

The method does NOT return the result set.

Instead you can use the PDOStatement object, to fetch the results:

$currentURL = $post["media_url"];
$sql = "SELECT * FROM images WHERE imageURL = '$currentURL'";
$result = $conn->query($sql)->fetchAll();

{ ... }

In case you are using mysqli, the object returned by query() is this:

So the code would be:

$currentURL = $post["media_url"];
$sql = "SELECT * FROM images WHERE imageURL = '$currentURL'";
$result = $conn->query($sql)->fetch_all(MYSQLI_ASSOC);

{ ... }

Please also note: Your code is highly insecure! You should use prepared statements to prevent sql-injection:

$currentURL = $post["media_url"];
$sql = "SELECT * FROM images WHERE imageURL = :currentUrl";
$stmt = $conn->prepare($sql);
$stmt->execute(['currentUrl' => $currentURL]);
$result = $stmt->fetchAll();

{ ... }

How to check if $row['column_name'] is returning empty php mysql

You can use ==0, this will check if it equals to 0:

if ($row['describe']==0) { /* code to do */ }

Or empty(), this will check if it is empty:

if (empty($row['describe'])) { /* code to do */ }

Personally, I would prefer !empty() as this will check if the variable is empty.

Hope this helps, thanks!

How to check fetched result set is empty or not?

Check $data variable like:

if ($data) {
//not empty
} else {
// empty

If the result of SELECT query did return any data, variable $data will contain a non-empty array/object which evaluates to true, and a false-like value otherwise.

mySQL returns result even though the database is empty

You are saying if($result)

This states that your if will always be true.

You need to say if($result == 1) for example.

You need to have $result equal, or not equal.

That is how an if statement works. However, you are saying "if query" which wont work anyways, you need to say something like,

if(mysqli_num_rows($result) > 0){
$user = $result->fetch_object();

Checking for an empty result (PHP, PDO, and MySQL)

You're throwing away a result row when you do $sth->fetchColumn(). That's not how you check if there are any results. You do

if ($sth->rowCount() > 0) {
... got results ...
} else {
echo 'nothing';

Relevant documentation is here: PDOStatement::rowCount

Related Topics

Leave a reply