Nanga
Nanga

Reputation: 73

How to check a table row is empty in PHP?

I want to check whether a row is empty or not in PHP. If "$jfeta3" is not empty, it gives the value of "Success", otherwise it goes to the else part. Inside the else part, if "$jfeta['rate_number']" is greater than 0, it gives the value of "Success2", otherwise it gives the value of "Success3". But all values are printing (Success, Success2 and Success3) at the sametime. I want to print only one value at one time.

Here is my PHP code.

$product_id = mysql_real_escape_string($_GET['p']);

$jsqla = mysql_query("select * from products where id='$product_id'") or die(mysql_error());
$jfeta = mysql_fetch_assoc($jsqla);

$product_id = $jfeta['id'];
$jsqla3 = mysql_query("select * from user_star_rate where product_id='$product_id' and      email='$visit_email'") or die(mysql_error());
$jfeta3 = mysql_fetch_assoc($jsqla3);

if(!is_null($jfeta3)) {
   $ratea = $jfeta3['rate_value'];
   echo "Success = ".$ratea;
   $rateid = $jfeta['id'];
   $arate_num = $jfeta['rate_number'];
} else {
   $arate_num = $jfeta['rate_number'];
   if($jfeta['rate_number'] > 0){ 
       $ratea = $jfeta['rate_score'] / $jfeta['rate_number']; 
       echo "Success2 = ".$ratea;
       $ratea2 = $jfeta['rate_score'];
       $rateid = $jfeta['id'];
       $ratenum = $jfeta['rate_number'];
   }else{ 
       $ratea = $jfeta['rate_score']; 
       echo "Success3 = ".$ratea;
       $ratea2 = $jfeta['rate_score'];
       $rateid = $jfeta['id'];
       $ratenum = $jfeta['rate_number'];
   }
}

Upvotes: 0

Views: 84

Answers (4)

ghostzali
ghostzali

Reputation: 42

Or you can try using mysql_num_rows() > 0 to replace !is_null(). I've try this code :

$product_id = mysql_real_escape_string($_GET['p']);

$jsqla = mysql_query("select * from products where id='$product_id'") or die(mysql_error());
$jfeta = mysql_fetch_assoc($jsqla);

$product_id = $jfeta['id'];
$jsqla3 = mysql_query("select * from user_star_rate where product_id='$product_id' and      email='$visit_email'") or die(mysql_error());
$jfeta3 = mysql_fetch_assoc($jsqla3);

if((mysql_num_rows($jsqla) > 0) && (mysql_num_rows($jsqla3) > 0)) {
   $ratea = $jfeta3['rate_value'];
   echo "Success = ".$ratea;
   $rateid = $jfeta['id'];
   $arate_num = $jfeta['rate_number'];
} else {
   $arate_num = $jfeta['rate_number'];
   if($jfeta['rate_number'] > 0){ 
       $ratea = $jfeta['rate_score'] / $jfeta['rate_number']; 
       echo "Success2 = ".$ratea;
       $ratea2 = $jfeta['rate_score'];
       $rateid = $jfeta['id'];
       $ratenum = $jfeta['rate_number'];
   }else{ 
       $ratea = $jfeta['rate_score']; 
       echo "Success3 = ".$ratea;
       $ratea2 = $jfeta['rate_score'];
       $rateid = $jfeta['id'];
       $ratenum = $jfeta['rate_number'];
   }
}

Upvotes: 0

Amare
Amare

Reputation: 715

Use if($jfeta3). It checks whether the statement is null or not. If it is not null or not empty it executes the if part, otherwise it executes the else part.

Upvotes: 0

Ian Hazzard
Ian Hazzard

Reputation: 7771

Your error is here: if (!is_null($jfeta3)). is_null() checks if a value is null, not empty. Simply use if ($jfeta3). If the value is null, the statement will not execute, and if the value is empty, the statement will still not execute.

Upvotes: 1

John Conde
John Conde

Reputation: 219834

is_null() is for checking if a value is null, not empty. To see if a value is empty use empty()

if(!empty($jfeta3)) {

It should be noted that null values are considered empty and will fail this check. Additional empty values are:

  • "" (an empty string)
  • 0 (0 as an integer)
  • 0.0 (0 as a float)
  • "0" (0 as a string)
  • FALSE
  • array() (an empty array)
  • $var; (a variable declared, but without a value)

Upvotes: 1

Related Questions