954,591 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

test mysql query for no results

Hey, I'm running a mysql query, and would like to run a script if the query returns no results. But what do I use to test for no results?

Something like:

[PHP]

if (!$query) {

do this

}

[/PHP]

But just not sure how to implement it. Little help?

nathanpacker
Posting Whiz in Training
234 posts since May 2005
Reputation Points: 10
Solved Threads: 0
 
if(mysql_num_rows($result)==0){
  //do this
}
php_daemon
Junior Poster
140 posts since Aug 2006
Reputation Points: 13
Solved Threads: 2
 
if(mysql_num_rows($result)==0){
  //do this
}


Thanks, that was perfect.

nathanpacker
Posting Whiz in Training
234 posts since May 2005
Reputation Points: 10
Solved Threads: 0
 
if(mysql_num_rows($result)==0){
  //do this
}


OMG, you helped me so much!! I am new to php, and I was tearing my hair out. Thank you, Thankyou,! You are my hero!!!!! :)

1supergirl
Newbie Poster
3 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
 

Will this work for odbc_num_rows also. I tried but I get the same message no matter what

$sql="SELECT * FROM rac_master WHERE mrnumb='$mrnumb' AND racnumb='$racnumb' AND findetdate IS NULL";
   $resultx=odbc_exec($conna,$sql);
       if (odbc_num_rows($resultx)==0) {
           echo"Record Has Been Finalized";
           }
           odbc_close($conna);
jamesmjr
Newbie Poster
4 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

Can't say for sure, but one way to find out would just be to test it. Echo that variable out and see what you get.

nathanpacker
Posting Whiz in Training
234 posts since May 2005
Reputation Points: 10
Solved Threads: 0
 

It returns "Record Has Been Finalized", even if the query statement is met. Does it matter what data type is being used in the table

jamesmjr
Newbie Poster
4 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 
It returns "Record Has Been Finalized", even if the query statement is met. Does it matter what data type is being used in the table

Wish I could help you here. I'm a novice at this stuff. Maybe someone more experienced will chime in.

nathanpacker
Posting Whiz in Training
234 posts since May 2005
Reputation Points: 10
Solved Threads: 0
 

Turns out the odbc_num_rows will always return -1 when using MSSQL. I changed my connecton to mssql_connect and mssql_num_rows . Every thing is working fine now.

Example:

$conn=mssql_connect('localhost','******','******');
 $msdb=mssql_select_db("DatabaseName",$conn);
 $sql="SELECT * FROM Table WHERE value='$variable' ";
 $result=mssql_query($sql,$conn);
         IF (mssql_num_rows($result)==0)  {
           echo"What ever";
           }
           mssql_close($conn);
jamesmjr
Newbie Poster
4 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

I was struggling with checking my empty results set before I loop with a while block until I found the above num_rows code and it worked right off. I think I spent 3 hours on this...amazing! Now I need to redirect back to the login page but this too is a struggle because I am using php header location and it's hard because I read that you are supposed to call before anything else. I'll get it though.

Thanks everyone!!!

c++learner
Light Poster
27 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 
$query = "SELECT * FROM table";
$results = mysql_query($query);
if($results) {
  //do something
}
else {
  //do something different
}


mysql_query will return false if no records are returned. That false will be propagated into your variable. You can test for that. Try to use a smaller, quicker query; you don't want to tie up too many system resources if you don't end up needing the results of the query.

BradChesney79

BradChesney79
Newbie Poster
2 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 
mysql_query will return false if no records are returned.

This is incorrect. mysql_query will return true (or a resource) if the query succeeds. Even if there are no results, the query is still successful.

If mysql_query returns false , then the query is incorrect (or the user does not have permission).

pritaeas
Posting Expert
Moderator
5,484 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 
$query = "SELECT * FROM table"; 
$results = mysql_query($query);
if ($results) { // query executed
  $row=mysql_fetch_array($results) { // get an initial row array
    if($row) { // query returned at least one row
      do {
        // the existence of $row is true, do something
      } while ($row=mysql_fetch_array($results));
    }
    else {
      // the existence of $row is false, do something different
      // this is where you can put some code if your successful query returned 0 rows
    }
  }
  else {
    // query failed
    // I choose to send myself the error information
    // I try to provide some non-specific feedback to the user explaining a failure
  }
}


The row is where you can get a false when it is empty.

I recently needed to test for this and wanted to see if there was a more elegant way to accomplish the end result I was looking for. In a way, it did allow me to make my php more robust, but it did not allow me to reduce the lines of code.

BradChesney79
Newbie Poster
2 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You