在PHP中,为什么mysqli_num_rows对于一个有0条返回记录的查询不返回整数?[英] In PHP, why doesn't mysqli_num_rows return an integer for a query with 0 returned rows?

本文是小编为大家收集整理的关于在PHP中,为什么mysqli_num_rows对于一个有0条返回记录的查询不返回整数?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我对mysqli_num_rows有一个奇怪的问题.在搜索此问题时,我只发现人们遇到无效的问题,无论如何都会返回.我还检查了官方文档说它返回查询返回的行数的整数.每当我的查询返回1行(不应该返回更多)时,它的行为就会如我所期望的那样.当查询返回0行时,我希望该函数返回0,但返回null.为什么不返回0?

我知道我的数据库连接很好,而且我的查询正常工作,因为当我寻找数据库中的记录时,我会恢复整数.我只是不知道为什么这是返回空而不是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似乎为无效的情况的一些代码:

<?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()之类的函数可以返回 是结果对象,也可以返回boolean stalcor. .

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

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

关于查询的一些内容导致错误.由您决定错误并报告.


更新:我编辑了上面的一些文本,以使解释更好,但是它可能会使下面的一些评论似乎不合时宜.

其他推荐答案

我只是不知道为什么这是返回null而不是0.

我们只能在不看到日志输出的情况下进行猜测;但是,这可能是返回值无效的,因为它会引起错误.

您需要在尝试使用返回值之前确保在调用功能时处理错误.

本文地址:https://itbaoku.cn/post/1669981.html

问题描述

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?

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;
    }
}

推荐答案

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

Output:

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

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.

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.

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.

其他推荐答案

I just can't figure out why this is returning NULL rather than 0.

We can only guess without seeing the log output; but, it is likely the return value is null because it raised an error instead.

You need to ensure that errors are handled when calling a function, before attempting to use the return value.