Jump to content

Checking an empty result


ainoy31

Recommended Posts

What is the best way to check for a result set that is empty? I have

the following:

 

$query = "SELECT * FROM nHeal WHERE order_id='$order_id'";

 

$result = mysql_query($query) or die(mysql_error());

 

$row = mysql_fetch_array($result);

 

if(empty($row))

{

header("Location: https://xxx.com/nHeal_ainoy/orderform.php");

}

else

{

      ......etc

}

 

it does not redirect as i wanted it.

 

thx.

Link to comment
https://forums.phpfreaks.com/topic/58559-checking-an-empty-result/
Share on other sites

<?php

  $query = "SELECT * FROM nHeal WHERE order_id='$order_id'";
  if ($result = mysql_query($query)) {
    if (mysql_num_rows($result)) {
      $row = mysql_fetch_array($result);
      // etc
    } else {
      header("Location: https://xxx.com/nHeal_ainoy/orderform.php");
    }
  } else {
    echo mysql_error();
  }

?>

I am trying to check if it returns 0 rows.  I guess the empty() function does not handle it

 

Your problem is that mysql_query returns true / false depending on query success / failure, not the amount of rows returned.

 

You need to use mysql_num_rows to see how many rows where returned by a sucessfull query.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.