CakeFest 2024: The Official CakePHP Conference

Sentencias Múltiples

MySQL permite opcionalmente tener múltiples sentencias en una cadena de sentencias. El envío de múltiples sentencias de una sola vez reduce los viajes de ida y vuelta desde el cliente al servidor, pero requiere un manejo especial.

Las sentencias múltiples o multiconsultas deben ser ejecutadas con mysqli_multi_query(). Las sentencias individuales de la cadena de sentencias están serparadas por un punto y coma. Entonces, todos los conjuntos de resultados devueltos por las sentencias ejecutadas deben ser obtenidos.

El servidor MySQL permite tener sentencias que devuelven conjuntos de resultados y sentencias que no devuelve conjuntos de resultados en una sentencia múltiple.

Ejemplo #1 Sentencias múltiples

<?php
$mysqli
= new mysqli("ejemplo.com", "usuario", "contraseña", "basedatos");
if (
$mysqli->connect_errno) {
echo
"Falló la conexión a MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}

if (!
$mysqli->query("DROP TABLE IF EXISTS test") || !$mysqli->query("CREATE TABLE test(id INT)")) {
echo
"Falló la creación de la tabla: (" . $mysqli->errno . ") " . $mysqli->error;
}

$sql = "SELECT COUNT(*) AS _num FROM test; ";
$sql.= "INSERT INTO test(id) VALUES (1); ";
$sql.= "SELECT COUNT(*) AS _num FROM test; ";

if (!
$mysqli->multi_query($sql)) {
echo
"Falló la multiconsulta: (" . $mysqli->errno . ") " . $mysqli->error;
}

do {
if (
$resultado = $mysqli->store_result()) {
var_dump($resultado->fetch_all(MYSQLI_ASSOC));
$resultado->free();
}
} while (
$mysqli->more_results() && $mysqli->next_result());
?>

El resultado del ejemplo sería:

array(1) {
  [0]=>
  array(1) {
    ["_num"]=>
    string(1) "0"
  }
}
array(1) {
  [0]=>
  array(1) {
    ["_num"]=>
    string(1) "1"
  }
}

Consideraciones de seguridad

Las funciones de la API mysqli_query() y mysqli_real_query() no establecen una bandera de conexión necesaria para activar las multiconsultas en el servidor. Se usa una llamada extra a la API para las sentencias múltiples para reducir la verosimilitud de los ataques de inyecciones SQL accidentales. Un atacante puede intentar añadir sentencias como ; DROP DATABASE mysql o ; SELECT SLEEP(999). Si el atacante tiene éxito al añadir SQL a la cadena de sentencias pero no se usa mysqli_multi_query, el servidor no ejecutará la segunda sentencia SQL inyectada y maliciosa.

Ejemplo #2 Inyección SQL

<?php
$mysqli
= new mysqli("ejemplo.com", "usuario", "contraseña", "basedatos");
$resultado = $mysqli->query("SELECT 1; DROP TABLE mysql.user");
if (!
$resultado) {
echo
"Error al ejecutar la consulta: (" . $mysqli->errno . ") " . $mysqli->error;
}
?>

El resultado del ejemplo sería:

Error al ejecutar la consulta: (1064) You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version for the right syntax 
to use near 'DROP TABLE mysql.user' at line 1

Sentencias preparadas

El uso de sentencias múltiples con sentencias preparadas no está soportado.

See also

add a note

User Contributed Notes 1 note

up
9
velthuijsen
5 years ago
Suggested improvement(s) to example 1.

reasons:
Multi_query only returns a non false response if a data/result set is returned and only checks for the first query entered. Switching the first SELECT query with the INSERT query will result in a premature exit of the example with the message "Multi query failed: (0)".
The example assumes that once the first query doesn't fail that the other queries have succeeded as well. Or rather it just exits without reporting that one of the queries after the first query failed seeing that if a query fails next_result returns false.

The changes in the example comes after the creation of the string $sql.

<?php
$mysqli
= new mysqli("example.com", "user", "password", "database");
if (
$mysqli->connect_errno) {
echo
"Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}

if (!
$mysqli->query("DROP TABLE IF EXISTS test") || !$mysqli->query("CREATE TABLE test(id INT)")) {
echo
"Table creation failed: (" . $mysqli->errno . ") " . $mysqli->error;
}

$sql = "SELECT COUNT(*) AS _num FROM test; ";
$sql.= "INSERT INTO test(id) VALUES (1); ";
$sql.= "SELECT COUNT(*) AS _num FROM test; ";

// changes to example 1 start here

// don't bother checking the result from multi_query since it will return false
// if the first query does not return data even if the query itself succeeds.
$mysqli->multi_query($sql);

do
// while (true); // exit only on error or when there are no more queries to process
{
// check if query currently being processed hasn't failed
if (0 !== $mysqli->errno)
{
echo
"Multi query failed: (" . $mysqli->errno . ") " . $mysqli->error;
break;
}

// store and possibly process result of the query,
// both store_result & use_result will return false
// for queries that do not return results (INSERT for example)
if(false !== ($res = $mysqli->store_result() )
{
var_dump($res->fetch_all(MYSQLI_ASSOC));
$res->free();
}

// exit loop if there ar no more queries to process
if (false === ($mysqli->more_results() )
{
break;
}

// get result of the next query to process
// don't bother to check for success/failure of the result
// since at the start of the loop there is an error check &
// report block.
$mysqli->next_result()

} while (
true); // exit only on error or when there are no more queries to process
?>

Note that the normal while ($mysqli->more_results() && $mysqli->next_result() has been replaced by two checks and while (true);
This is due to the 'problem' that next_result will return false if the query in question failed.
So one either needs to do one last check after the while loop to check if there was an error or one has to split up the different actions.
The changes in the example do the splitting.
To Top