ashutoash Posted August 30, 2011 Share Posted August 30, 2011 I am using PDO to run query's and get data. How do I check if the PDO query returned a null value. I tried empty but that doesn't work because PDO array will always contain an SQL query. I want to check if the return value of that query is NULL :confused: . Help please. Quote Link to comment Share on other sites More sharing options...
cunoodle2 Posted August 30, 2011 Share Posted August 30, 2011 Can you see if it is NULL??? Try a var_dump() on the values as likely it is an array Quote Link to comment Share on other sites More sharing options...
ashutoash Posted August 30, 2011 Author Share Posted August 30, 2011 I want to see if the return value is NULL. Based on that I want to display a NO DATA label on my front end. But I am unable to do it. Hence this question. Quote Link to comment Share on other sites More sharing options...
cunoodle2 Posted August 30, 2011 Share Posted August 30, 2011 Can you show your code? Quote Link to comment Share on other sites More sharing options...
ashutoash Posted August 30, 2011 Author Share Posted August 30, 2011 @cunoodle2: Here is what I am doing. Its a simple query: $result_download = $db->query("SELECT download_hits as total_hits from date1_dcerpc where time = 1314712800 "); I know there are no download_hits at 1314712800 as there is no entry for 1314712800 in the table so the return should be null. I want to know how to check if the PDO object returns null. Quote Link to comment Share on other sites More sharing options...
Jet4Fire Posted August 30, 2011 Share Posted August 30, 2011 check errorCode() example: if ($db->errorCode() == '00000') { // do something } else { // display error } Quote Link to comment Share on other sites More sharing options...
cunoodle2 Posted August 30, 2011 Share Posted August 30, 2011 Technically your query will NOT return NULL. It will return zero results. From the Manual: Counting rows returned by a SELECT statement 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 rows that will be returned. Your application can then perform the correct action. <?php $sql = "SELECT download_hits as total_hits from date1_dcerpc where time = 1314712800"; if ($res = $conn->query($sql)) { /* Check the number of rows that match the SELECT statement */ if ($res->fetchColumn() > 0) { /* Issue the real SELECT statement and work with the results */ $sql = "SELECT download_hits as total_hits from date1_dcerpc where time = 1314712800"; foreach ($conn->query($sql) as $row) { print "Name: " . $row['download_hits'] . "\n"; } } /* No rows matched -- do something else */ else { print "No rows matched the query."; } } $res = null; $conn = null; ?> Quote Link to comment Share on other sites More sharing options...
cunoodle2 Posted August 30, 2011 Share Posted August 30, 2011 I may have botched that a little on the post. Here is the link to the page that I got that info from.. http://php.net/manual/en/pdostatement.rowcount.php Quote Link to comment Share on other sites More sharing options...
ashutoash Posted August 30, 2011 Author Share Posted August 30, 2011 Thanks cunoodle2 and Jet4Fire. The errorcode thing did not work but I am doing a count of rows and if count == 0, I am displaying ND. . This is solved. But most of my PDO statments, if there is no data, give out a warning PHP Warning: Invalid argument supplied for foreach(). I read somewhere that @ can be used to suppress these but that doesn't work. How can I suppress these? Those warnings are filling up my logs like anything. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.