doudu22272099831
doudu22272099831
2017-12-20 07:48
浏览 116
已采纳

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

I have a mysql query that I get data from mysql table. How can I properly check if there is result returned? My mysql table have 5 rows with data.

I have tried to equate mysqli_num_rows to 0 but it does not work. It always return fail. I need it to return success.

I can't figure this out, how can I fix this?

my code:

$result=mysqli_num_rows($query)

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

图片转代码服务由CSDN问答提供 功能建议

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

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

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

我的代码:

  $ result = mysqli_num_rows($ query)
 
if($ result == 0)
 {  
 echo“Success”; 
} else 
 {
 echo“fail”; 
} 
   
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douzhang5121
    douzhang5121 2017-12-20 07:55
    已采纳

    Your statement only evaluates to true if mysqli_num_rows is equal to 1:

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

    You should check if it is equal more than 0, so that it will evaluate true when it returns more than 1 row:

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

    Usage of mysqli_num_rows():

    Returns the number of rows in the result set.

    点赞 评论
  • dongliu4320
    dongliu4320 2017-12-20 07:52

    you have an issue with this line

    result==0 //also no $ sign here
    
     //try like this
    
    if ($result){
    
    }
    

    hope so it will help

     <?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();
    
        ?>
    

    for more please read this 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
        }
    
    点赞 评论

相关推荐