2

I'm writing a class in PHP for handling MySQLI queries, and when the query manages not to return a result set (or object) I want to issue a helpful error message. But, of course, not all queries are designed to return a result set (e.g. ALTER), so I want to filter out those queries that don't.

By my reckoning, the queries that may or may not return a result set are:

  • SELECT
  • SHOW
  • CREATE VIEW

...and all others (e.g. ALTER) do NOT return a result. But have I overlooked something? What are the query commands that never return a result, and what are the query commands that may or may not return a result?

Many thanks!

2 Answers2

4

From the PHP manual for mysqli::query

Return Values

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.

I suggest to just check if the return value is equal to false (=== false) and do something with mysqli_errno or mysqli_error if it is. Actual parsing of the query string is overkill.

If you must know whether it is a result set or just a boolean, check for the return type with is_bool($result) or $result instanceof mysqli_result.

Community
  • 1
  • 1
Basti
  • 3,998
  • 1
  • 18
  • 21
0

There are two types basically:

  1. Ones that return a result set - it may vary from empty set to a massive amount of data
  2. Ones that return a count of affected rows.

There are also three types of queries:

  1. DML - queries to manipulate data - select, insert, update, delete.
  2. DDL - queries to figure out data schema - create table/view etc.
  3. Control queries - grant or revoke permissions

I suppose control queries usually return nothing and DDL queries like create/alter/set also return nothing. SELECT returns result sets (which may be empty). INSERT/UPDATE/DELETE return the number of rows affected.

I'd recommend reading on SQL queries for more information.

Dmitry Reznik
  • 6,812
  • 2
  • 32
  • 27