将mysqli_num_rows等同于0但它不起作用

时间:2017-12-20 07:48:09

标签: php mysql

我有一个mysql查询,我从mysql表中获取数据。如何正确检查是否有结果返回?我的mysql表有5行数据。

我试图将mysqli_num_rows等同于0,但它不起作用。它总是会失败。我需要它才能恢复成功。

我无法解决这个问题,我该如何解决这个问题?

我的代码:

$result=mysqli_num_rows($query)

if($result==0)
{
    echo "Success";
}else
{
    echo "fail";
}

2 个答案:

答案 0 :(得分:1)

这行有问题

result==0 //also no $ sign here

 //try like this

if ($result){

}

希望如此有用

 <?php
    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 

    $sql = "SELECT * FROM Table";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
        // data is available 

    } else {
        echo "0 results";
    }

    $conn->close();

    ?>

更多请阅读此内容 php_mysql_data

OR

  $q='query here';
    $result=mysqli_query($con,$q);
    if (!$result)
        echo(mysqli_error($con));

    if(mysqli_num_rows($result)>0){
    //result here
    }

答案 1 :(得分:0)

如果true等于mysqli_num_rows,您的陈述仅评估为1

if($test = mysqli_num_rows($result) == 0){

你应该检查它是否等于0,这样当它返回超过1行时它会评估为真:

$result = mysqli_num_rows($query)

if ($result == 0) {
    echo "Success";
} else {
    echo "fail";
}

mysqli_num_rows()的用法:

  

返回结果集中的行数。