php mysqli_query - 返回值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14419593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:14:04  来源:igfitidea点击:

mysqli_query - return values

phpmysqli

提问by MadLarkin

I am using the PHP function mysqli_queryto run a SELECTquery.

我正在使用 PHP 函数mysqli_query来运行SELECT查询。

What does mysqli_queryreturn if the query runs successfully, but the query finds no matches?

mysqli_query如果查询运行成功,但查询没有找到匹配项,会返回什么?

回答by AgentConundrum

Per the manual:

根据手册

Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.

失败时返回 FALSE。对于成功的 SELECT、SHOW、DESCRIBE 或 EXPLAIN 查询,mysqli_query() 将返回一个 mysqli_result 对象。对于其他成功的查询,mysqli_query() 将返回 TRUE。

A query that runs but returns no results is still considered a "successful query", since the query did run in the database and an empty result set is a legitimate response. This means the query will still return a mysqli_resultobject, and if you check its row count (either via $result->num_rowsin the object-oriented style or mysqli_num_rows($result)in the procedural style) it will return 0.

运行但不返回结果的查询仍被视为“成功查询”,因为该查询确实在数据库中运行并且空结果集是合法响应。这意味着查询仍然会返回一个mysqli_result对象,如果你检查它的行数(通过$result->num_rows面向对象风格或mysqli_num_rows($result)过程风格),它将返回 0。

回答by Niels

A Mysqli_queryobject, than you can use mysqli_num_rowsto count the number of rows returned. So:

一个Mysqli_query对象,比你可以使用mysqli_num_rows来计算返回的行数。所以:

if(mysqli_num_rows($query) > 0 ){
    // Do something
}

回答by Tommaso Belluzzo

if ($result = $mysqli->query("SELECT * FROM data"))
{
    $count = $result->num_rows; 
    printf("Result set has %d rows.\n", $count);
    $result->close();
}

From reference:

参考

Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.

失败时返回 FALSE。对于成功的 SELECT、SHOW、DESCRIBE 或 EXPLAIN 查询,mysqli_query() 将返回一个 mysqli_result 对象。对于其他成功的查询,mysqli_query() 将返回 TRUE。