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

In PHP, you can use the PDO (PHP Data Objects) extension to interact with a MySQL database. To check if a query returned an empty result set, you can use the rowCount() method on the PDO statement object. This method returns the number of rows affected by the query, so if the value is 0, it means the query returned an empty result set.

Example:

<?php

$stmt = $pdo->query('SELECT * FROM users WHERE id = 1');
if ($stmt->rowCount() == 0) {
  echo "No results found.";
} else {
  // process the results
}

Watch a course Learn object oriented PHP

You can also use fetchAll() method on the PDO statement object which will return an empty array if the query returned no results.

<?php

$stmt = $pdo->query('SELECT * FROM users WHERE id = 1');
$result = $stmt->fetchAll();
if(empty($result)){
    echo "No Results Found.";
}