Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysqli_query - return values

Tags:

php

mysqli

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?

like image 868
MadLarkin Avatar asked Jan 19 '13 22:01

MadLarkin


People also ask

What does mysqli_query return if empty?

For other successful queries mysqli_query() will return TRUE. The result can still be empty even if it was succesful. If you use pdo, as suggested above. You get an array back (empty array if result is empty) so you can do sizeof($array) or count($array) to check if you have 0 results or not.

What does the mysqli_query () function do?

Definition and Usage. The mysqli_query() function accepts a string value representing a query as one of the parameters and, executes/performs the given query on the database.

What does mysqli_num_rows return?

The mysqli_num_rows() function returns the number of rows in a result set.

What does mysqli_connect return?

The return value of mysqli_connect() is a database connection "handle". The handle is an object which represents the connection to the database.


1 Answers

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.

like image 103
AgentConundrum Avatar answered Sep 24 '22 15:09

AgentConundrum