Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

mysql

pdo

What am I doing wrong here? I'm simply retrieving results from a table and then adding them to an array. Everything works as expected until I check for an empty result...

This gets the match, adds it to my array and echoes the result as expected:

$today = date('Y-m-d', strtotime('now'));  $sth = $db->prepare("SELECT id_email FROM db WHERE hardcopy = '1' AND hardcopy_date <= :today AND hardcopy_sent = '0' ORDER BY id_email ASC");  $sth->bindParam(':today', $today, PDO::PARAM_STR);  if(!$sth->execute()) {     $db = null;     exit(); }  while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {     $this->id_email[] = $row['id_email'];     echo $row['id_email']; }  $db = null; return true; 

When I try to check for an empty result, my code returns 'empty', but no longer yields the matching result:

$today = date('Y-m-d', strtotime('now'));  $sth = $db->prepare("SELECT id_email FROM db WHERE hardcopy = '1' AND hardcopy_date <= :today AND hardcopy_sent = '0' ORDER BY id_email ASC");  $sth->bindParam(':today',$today, PDO::PARAM_STR);  if(!$sth->execute()) {     $db = null;     exit(); }  if ($sth->fetchColumn()) {     echo 'not empty';     while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {         $this->id_email[] = $row['id_email'];         echo $row['id_email'];     }     $db = null;     return true; } echo 'empty'; $db = null; return false; 
like image 895
canoebrain Avatar asked Nov 20 '12 16:11

canoebrain


People also ask

How do you check if SQL result is empty in PHP?

The easiest way is to use mysql_num_rows(). $cnt = mysql_num_rows($Result); if ( 0===$cnt ) { echo 'no records'; } else { while( false!=( $row=mysql_fetch_array($Result)) ) { // ... } }

How do I check if an array is empty in mysql?

if you are using mysql use mysql_num_rows() .. this will give you the count of fetched results from table .. so if the count is 0 than your array is empty.... or you are using PDO than use PDOstatement->rowCount() ... and if you are using mysqli than use mysqli_num_rows to get the count...

How do I find the number of rows in PDO?

For most databases, PDOStatement::rowCount() does not return the number of rows affected by a SELECT statement. Instead, use PDO::query() to issue a SELECT COUNT(*) statement with the same predicates as your intended SELECT statement, then use PDOStatement::fetchColumn() to retrieve the number of matching rows.

What is PDO query in PHP?

PDO::query() prepares and executes an SQL statement in a single function call, returning the statement as a PDOStatement object.

What happens if mysql query results are empty?

If MySQL query results are empty then else condition should not be executed. In case MySQL results in data there & in else condition my error my message is there but it is not showing any error message.

Why to check both ISSET() and !empty() function in PHP?

- GeeksforGeeks Why to check both isset () and !empty () function in PHP ? The isset () function is an inbuilt function in PHP which checks whether a variable is set and is not NULL. This function also checks if a declared variable, array or array key has null value, if it does, isset () returns false, it returns true in all other possible cases.

How do I check for an empty result set in SQL?

There are a couple of ways that you can check for an empty result set. If you’re just wanting to get a single row, call the fetch method and then check to see if the return value is empty:

How to Access MySQL_* API in PHP?

mysql_* API has been removed from PHP long time ago. To access the database you should use PDO. Checking if PDO has returned any results is actually pretty simple. Just fetch the results and if the array is empty then there was nothing returned from MySQL. $stmt = $pdo->prepare ('SELECT * FROM hall_search_data_1 WHERE rent BETWEEN ?


1 Answers

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

like image 161
Marc B Avatar answered Sep 19 '22 16:09

Marc B