MySQLi Tutorial MySQLi References

mysqli real_query() Method



The mysqli::real_query() / mysqli_real_query() function is used to execute a single query against the database whose result can then be retrieved or stored using the mysqli_store_result() or mysqli_use_result() functions.

In order to determine if a given query returns a result set or not, mysqli_field_count() function can be used.

Syntax

//Object-oriented style
public mysqli::real_query(query)

//Procedural style
mysqli_real_query(mysql, query)

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.

Return Value

Returns true on success or false on failure.

Example: Object-oriented style

The example below shows the usage of mysqli::real_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();
}

//executing an SQL query
$sql = "SELECT EmpID, Name, Age FROM Employee ORDER BY Age";
$mysqli->real_query($sql);

if ($mysqli->field_count) {
  //transferring the result set from the last query
  $result = $mysqli->store_result();

  //processing the resultset
  $row = $result->fetch_row();

  //free the memory associated with the result
  $result->close();
}

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

Example: Procedural style

The example below shows the usage of mysqli_real_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();
}

//executing an SQL query
$sql = "SELECT EmpID, Name, Age FROM Employee ORDER BY Age";
mysqli_real_query($mysqli, $sql);

if (mysqli_field_count($mysqli)) {
  //transferring the result set from the last query
  $result = mysqli_store_result($mysqli);

  //processing the resultset
  $row = mysqli_fetch_row($result);

  //free the memory associated with the result
  mysqli_free_result($result);
}

//closing the connection
mysqli_close($mysqli);
?>

❮ MySQLi Functions Reference