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;
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)) ) { // ... } }
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...
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.
PDO::query() prepares and executes an SQL statement in a single function call, returning the statement as a PDOStatement object.
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.
- 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.
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:
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 ?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With