MySQLi Tutorial MySQLi References

mysqli query() Method



The mysqli::query() / mysqli_query() function is used to perform a query on the database.

For non-DML queries (not INSERT, UPDATE or DELETE), this function is similar to calling mysqli_real_query() followed by either mysqli_use_result() or mysqli_store_result().

Note: When passing a statement longer than max_allowed_packet of the server, this function returns different error codes depending on whether MySQL Native Driver (mysqlnd) or MySQL Client Library (libmysqlclient) is being used. The behavior is as follows:

  • mysqlnd on Linux returns an error code of 1153. The error message means got a packet bigger than max_allowed_packet bytes.
  • mysqlnd on Windows returns an error code 2006. This error message means server has gone away.
  • libmysqlclient on all platforms returns an error code 2006. This error message means server has gone away.

Syntax

//Object-oriented style
public mysqli::query(query, result_mode)

//Procedural style
mysqli_query(mysql, query, result_mode)

Parameters

mysql Required. For procedural style only: Specify a mysqli object returned by mysqli_connect() or mysqli_init().
query Required. Specify the SQL query string.
result_mode Optional. Specify the result mode. It indicates how the result will be returned from the MySQL server. It can be one of the following:
  • MYSQLI_STORE_RESULT (default) - returns a mysqli_result object with buffered result set.
  • MYSQLI_USE_RESULT - returns a mysqli_result object with unbuffered result set. As long as there are pending records waiting to be fetched, the connection line will be busy and all subsequent calls will return error: Commands out of sync. To avoid the error all records must be fetched from the server or the result set must be discarded by calling mysqli_free_result().
  • MYSQLI_ASYNC (available with mysqlnd) - the query is performed asynchronously and no result set is immediately returned. mysqli_poll() is then used to get results from such queries. Used in combination with either MYSQLI_STORE_RESULT or MYSQLI_USE_RESULT constant.

Return Value

Returns false on failure. For successful queries which produce a result set, such as SELECT, SHOW, DESCRIBE or EXPLAIN, this function will return a mysqli_result object. For other successful queries, this function will return true.

Example: Object-oriented style

The example below shows the usage of mysqli::query() method.

<?php
//establishing connection to the database
$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_errno) {
  echo "Failed to connect to MySQL: ". $mysqli->connect_error;
  exit();
}

//getting query result from the database
$sql = "SELECT EmpID, Name, Age FROM Employee ORDER BY Age";
$result = $mysqli->query($sql);

//closing the connection
$mysqli->close();

//getting the number of rows in the result set
$row_cnt = $result->num_rows;
printf("Result set has %d rows.\n", $row_cnt);
?>

The output of the above code will be similar to:

Result set has 58 rows.

Example: Procedural style

The example below shows the usage of mysqli_query() function.

<?php
//establishing connection to the database
$mysqli = mysqli_connect("localhost", "user", "password", "database");
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: ". mysqli_connect_error();
  exit();
}

//getting query result from the database
$sql = "SELECT EmpID, Name, Age FROM Employee ORDER BY Age";
$result = mysqli_query($mysqli, $sql);

//closing the connection
mysqli_close($mysqli);

//getting the number of rows in the result set
$row_cnt = mysqli_num_rows($result);
printf("Result set has %d rows.\n", $row_cnt);
?>

The output of the above code will be similar to:

Result set has 58 rows.

❮ MySQLi Functions Reference