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:51

    <?php
    $servername = "localhost";
    $username = "username";
    $password = "password";
    $dbname = "myDB";
    $conn = new mysqli($servername, $username, $password, $dbname);
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    $sql = "SELECT id, firstname, lastname FROM MyGuests";
    $result = $conn->query($sql);
    if ($result->num_rows > 0) {
        while ($row = $result->fetch_assoc()) {
            echo "id: " . $row["id"] . " - Name: " . $row["firstname"] . " " . $row["lastname"] . "<br>";
        }
    } else {
        echo "0 results";
    }
    $conn->close();
    ?>

查看更多
贪生不怕死
3楼-- · 2020-02-06 22:51

Security Tip First of all stop using the mysql_* functions because they are not secure for production and later versions has stopped support for this API. So if accidentally you used those function in production then you can be in trouble.

It is not recommended to use the old mysql extension for new development, as it was deprecated in PHP 5.5.0 and was removed in PHP 7. A detailed feature comparison matrix is provided below. More Read

For your answer you have to only check no of rows is zero or not Read this Post at php documentation with Example.

mysqli_num_rows

查看更多
Root(大扎)
4楼-- · 2020-02-06 22:52

Check number of rows

$result = mysqli_query($conn, $sql);
$rowcount=mysqli_num_rows($result); 
if($rowcount > 0){ 
  echo "Number of rows = " . $rowcount;
  }
  else
  { 
  echo "no record found"; 
  }
查看更多
仙女界的扛把子
5楼-- · 2020-02-06 22:54

You can use mysql_num_rows to get count of number of rows returned from query.

if(mysqli_num_rows($res) > 0)
{
    // rest of your stuff
}
else
{
    echo "No records found.";
}

Note: mysql is deprecated instead use mysqli or PDO as seen above

查看更多
▲ chillily
6楼-- · 2020-02-06 22:55

You can use mysql_num_rows to check the number of your result set:

    $sql = "select * from hall_search_data_1 where rent BETWEEN '".$_SESSION['amount1']."' AND '".$_SESSION['amount2']."'";
    $res = mysql_query($sql);
if(isset($res))
{
$noRows = mysql_num_rows($res);
    if($noRows > 0){
    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";
    }
}

Find here the documentation of mysql_num_rows:

mysql_num_rows

查看更多
▲ chillily
7楼-- · 2020-02-06 22:59

You can use mysql_num_rows(); to check your query return rows or not

$sql = "select * from hall_search_data_1 where rent BETWEEN '".$_SESSION['amount1']."' AND '".$_SESSION['amount2']."'";
$res = mysql_query($sql);
$rows=mysql_num_rows($res);
if($rows>0)
{
   echo "data return from query";
}else{

echo "data not return";
}

Note:- mysql is deprecated instead use mysqli or PDO

查看更多
登录 后发表回答