What is the difference between mysqli::query
and mysqli::real_query
?
OR
What is the difference between mysqli_query
and mysqli_real_query
?
What is the difference between mysqli::query
and mysqli::real_query
?
OR
What is the difference between mysqli_query
and mysqli_real_query
?
mysqli::query
will return a result if there is any.
mysql::real_query
will return true on success or false if not
You could have seen this in the php doc:
Look at the documentation of mysqli_query():
Functionally, using this function is identical to calling mysqli_real_query() followed either by mysqli_use_result() or mysqli_store_result().
From what I understand real_query actually executes the query, and use/store_result initiates the process of retrieving a result set for the query. query() does both.
Bit late, but the biggest advance for me is the RAM usage when these functions are called with default settings: with mysqli_real_query()
you do not copy the whole result into the RAM, which mysqli_query()
by default does (although it can be changed by using the $resultmode
parameter).
In practice there is another difference I don't see in other answers. You could need to use mysqli_real_query()
for a CALL
statement.
If you are calling a stored procedure, then it can return more than one result. mysqli_query()
will fetch the first result, but could be more results that needs to be fetched, and that would cause an error. You need to use mysqli_real_query
(or mysqli_multi_query()
) to fetch those result sets.
Sadly that explanation is in the stored procedures section of the PHP docs, so is hard to reach.
I let you a code example, just in case, calling a procedure that suppose to return several result sets using mysqli::real_query
:
$query="CALL storedProcedure()";
if($conn->real_query($query)){
do{
if($result=$conn->store_result()) {
while($row=$result->fetch_assoc()) {
print_r($row);
}
$result->free();
}
}while($conn->more_results() && $conn->next_result());
}