我想在PHP中检查行是否为空。如果“$ jfeta3”不为空,则给出“Success”的值,否则它将转到else部分。在else部分内部,如果“$ jfeta ['rate_number']”大于0,则它给出“Success2”的值,否则它给出“Success3”的值。但是所有值都在同一时间打印(Success,Success2和Success3)。我想一次只打印一个值。
这是我的PHP代码。
$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'];
}
}
答案 0 :(得分:1)
is_null()
用于检查值是null
,而不是空。要查看值是否为空,请使用empty()
if(!empty($jfeta3)) {
应该注意,null
值被视为空,并且将无法通过此检查。其他空值为:
答案 1 :(得分:1)
您的错误在于:if (!is_null($jfeta3))
。 is_null()
检查值是否为空,而不是空。只需使用if ($jfeta3)
即可。如果该值为null,则该语句将不会执行,如果该值为空,则该语句仍将不执行。
答案 2 :(得分:0)
使用if($jfeta3)
。它检查语句是否为null。如果它不为null或不为空,则执行if部分,否则执行else部分。
答案 3 :(得分:0)
或者您可以尝试使用mysql_num_rows() > 0
替换!is_null()
。
我试过这段代码:
$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'];
}
}