How to check if MySQL results returned empty in PH

2020-02-06 22:22发布

How to check mysql results are empty or not. if mysql query results are empty then else condtion should not be excueted. In case mysql results data there & in else condition my error my message are there but it is not showing any error message. I have tried following code but not showing any alert or echo message on screen.

<?php
$sql = "select * from hall_search_data_1 where rent BETWEEN '".$_SESSION['amount1']."' AND '".$_SESSION['amount2']."'";
$res = mysql_query($sql);
if(!empty($res)){
while($row=mysql_fetch_row($res))
{
// here my data 
}
}else{
echo "no results found";
echo "<br>";
echo "<script>alert('No any result found..!!');</script>";
echo "no results found";
}
?>

标签: php mysql
7条回答
放荡不羁爱自由
2楼-- · 2020-02-06 22:59

you can just replace if(!empty($res)) with mysql_num_rows like this -

$sql = "select * from hall_search_data_1 where rent BETWEEN '".$_SESSION['amount1']."' AND '".$_SESSION['amount2']."'";
$res = mysql_query($sql);
if($countrows = mysql_num_rows($res) >= 1){
    while($row=mysql_fetch_row($res))
    {
        here my data 
    }
}else{
    echo "no results found";
    echo "<br>";
    echo "<script>alert('No any result found..!!');</script>";
    echo "no results found";
}
查看更多
登录 后发表回答