mysqli_query - return values

2020-02-09 17:45发布

I am using the PHP function mysqli_query to run a SELECT query.

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

标签: php mysqli
3条回答
放我归山
2楼-- · 2020-02-09 18:01

A Mysqli_query object, than you can use mysqli_num_rows to count the number of rows returned. So:

if(mysqli_num_rows($query) > 0 ){
    // Do something
}
查看更多
闹够了就滚
3楼-- · 2020-02-09 18:02
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.

查看更多
做自己的国王
4楼-- · 2020-02-09 18:27

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.

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_result object, and if you check its row count (either via $result->num_rows in the object-oriented style or mysqli_num_rows($result) in the procedural style) it will return 0.

查看更多
登录 后发表回答