在PHP中,为什么mysqli_num_rows不为返回的行数为0的查询返回整数? [英] In PHP, why doesn't mysqli_num_rows return an integer for a query with 0 returned rows?

查看:43
本文介绍了在PHP中,为什么mysqli_num_rows不为返回的行数为0的查询返回整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用mysqli_num_rows时遇到了一个奇怪的问题.在搜索此问题时,我仅发现有人遇到无论如何都返回NULL的问题.我还检查了官方文档的功能,并且说它返回查询返回的行数的整数.每当我的查询返回1行(它永远不应该返回更多)时,它的行为就与我期望的一样.当查询返回0行时,我希望函数返回0,但它返回NULL.为什么它不返回0?

I am having a strange issue with mysqli_num_rows. Searching for this issue, I've only found people having issues where NULL is returned no matter what. I also checked the official documentation for the function, and it says it returns an integer of the number of rows returned by the query. Whenever my query returns 1 row (it never should return more), it behaves as I expect. When the query returns 0 rows, I expect the function to return 0, but it returns NULL. Why doesn't it return 0?

我知道我的数据库连接良好并且查询正常运行,因为当我查找数据库中的记录时,我会得到一个整数.我只是不知道为什么这会返回NULL而不是0.

I know that my database connection is good and my query works correctly, because when I look for a record that's in the database, I get an integer back. I just can't figure out why this is returning NULL rather than 0.

function getArtistID($name) {
    global $conn; 
    $query = "SELECT artist_id FROM artist WHERE artist_name LIKE '${name}'";
    $result = mysqli_query($conn, $query);
    if ($result->num_rows) {
        $row = mysqli_fetch_assoc($result);
        return $row['artist_id'];
    } else {
        return 0;
    }
}

推荐答案

以下是一些我用来重现num_rows似乎为NULL的情况的代码:

Here's some code that I used to reproduce a case where num_rows seems to be NULL:

<?php
error_reporting(E_ALL);
$conn = new mysqli('127.0.0.1', 'root', null, 'test');
$sql = "SELECT * FROM dual";
$result = $conn->query($sql);
if ($result === false) {
  print "Error: {$conn->error}\n";
}
$n = $result->num_rows;
echo "Dump the num_rows property: ";
var_dump($n);

输出:

Error: No tables used

Notice: Trying to get property of non-object in /Users/bkarwin/Documents/SO/my.php on line 14
Dump the num_rows property: NULL

该通知是因为访问不是对象的变量的面向对象属性无效.这是PHP开发人员经常感到困惑的原因,并且是PHP是一种松散类型的语言,并且query()之类的函数可以返回 一个结果对象或一个布尔标量的事实的副产品.

The notice is because it's invalid to access an object-oriented property of a variable that is not an object. This is a frequent source of confusion for PHP developers, and it's a byproduct of the fact that PHP is a loosely typed language, and functions like query() can return either a result object, or a boolean scalar.

由于某些错误,query()函数实际上返回了 false 作为$result.在我的代码中,我检查了此错误,而您没有.

The query() function actually returned a false as $result because of some error. In my code, I checked for this error, and you didn't.

运行mysqli::query()mysqli::prepare()mysqli_stmt::execute()时,必须每次检查错误条件.

When you run mysqli::query() or mysqli::prepare() or mysqli_stmt::execute(), you must check for error conditions every time.

关于您的查询的某些内容导致错误.由您检查错误并报告错误.

Something about your query caused an error. It's up to you to check for the error and report it.

更新:我在上面编辑了一些文字,以使说明更好,但可能会使下面的某些注释不合时宜.

Update: I edited some text above to make the explanation better, but it might make some comments below seem out of place.

这篇关于在PHP中,为什么mysqli_num_rows不为返回的行数为0的查询返回整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆