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:

if (!$query) {

do this

}

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

Recommended Answers

All 12 Replies

if(mysql_num_rows($result)==0){
  //do this
}
if(mysql_num_rows($result)==0){
  //do this
}

Thanks, that was perfect.

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!!!!! :)

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);

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.

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

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.

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);

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!!!

$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
<URL SNIPPED>

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).

$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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.