Deepak Kumar
Deepak Kumar

Reputation: 159

How to check fetched result set is empty or not?

For example:

$sql = 'select a, b, c from table where condition';
$stmt->prepare($sql);
$stmt->execute();
$data = $stmt->fetch();

AND

 $sql = 'select a, b, c from table where condition';
 $stmt->prepare($sql);
 $stmt->execute();
 $data = $stmt->fetchAll();

How to check if result set is empty or not

Upvotes: 3

Views: 4122

Answers (1)

Vladimir
Vladimir

Reputation: 1391

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.

Upvotes: 4

Related Questions