Forum und email

mysqli_query

mysqli->query

(PHP 5)

mysqli->query — Ejecuta una consulta en la base de datos

Descripción

Estilo por procedimientos:

mixto mysqli_query ( mysqli $identificador_de_enlace , cadena $consulta [, int $resultmode ] )

Estilo orientado a objetos (método):

mysqli
mixto query ( cadena $consulta )

La funció mysqli_query() es usada para simplificar la acción de ejecutar una consulta sobre la base de datos representada por el parámetro identificador_de_enlace .

Funcionalmente, usar está función es identifo a llamar mysqli_real_query() seguida ya sea de mysqli_use_result() o mysqli_store_result() donde consulta es la cadena de la consulta y resultmode es una de las constantes MYSQLI_USE_RESULT o MYSQLI_STORE_RESULT dependiendo del comportamiento deseado. Por defecto, si no se da resultmode , MYSQLI_STORE_RESULT es usado.

Si usted ejecuta mysqli_query() con resultmode MYSQLI_USE_RESULT todas las llamadas subsecuentes regresarán el código de error Commands out of sync a menos que llame mysqli_free_result().

Valores retornados

Devuelve TRUE si todo se llevó a cabo correctamente, FALSE en caso de fallo. Para SELECT, SHOW, DESCRIBE o EXPLAIN mysqli_query() regresará un objeto resultante.

Ejemplos

Example#1 Estilo orientado a objetos

<?php
$mysqli 
= new mysqli("localhost""my_user""my_password""world");

/* check connection */
if (mysqli_connect_errno()) {
    
printf("Connect failed: %s\n"mysqli_connect_error());
    exit();
}

/* Create table doesn't return a resultset */
if ($mysqli->query("CREATE TEMPORARY TABLE myCity LIKE City") === TRUE) {
    
printf("Table myCity successfully created.\n");
}

/* Select queries return a resultset */
if ($result $mysqli->query("SELECT Name FROM City LIMIT 10")) {
    
printf("Select returned %d rows.\n"$result->num_rows);

    
/* free result set */
    
$result->close();
}

/* If we have to retrieve large amount of data we use MYSQLI_USE_RESULT */
if ($result $mysqli->query("SELECT * FROM City"MYSQLI_USE_RESULT)) {

    
/* Note, that we can't execute any functions which interact with the
       server until result set was closed. All calls will return an 
       'out of sync' error */
    
if (!$mysqli->query("SET @a:='this will not work'")) {
        
printf("Error: %s\n"$mysqli->error);
    }
    
$result->close();
}

$mysqli->close();
?>

Example#2 Estilo por procedimientos

<?php
$link 
mysqli_connect("localhost""my_user""my_password""world");

/* check connection */
if (mysqli_connect_errno()) {
    
printf("Connect failed: %s\n"mysqli_connect_error());
    exit();
}

/* Create table doesn't return a resultset */
if (mysqli_query($link"CREATE TEMPORARY TABLE myCity LIKE City") === TRUE) {
    
printf("Table myCity successfully created.\n");
}

/* Select queries return a resultset */
if ($result mysqli_query($link"SELECT Name FROM City LIMIT 10")) {
    
printf("Select returned %d rows.\n"mysqli_num_rows($result));

    
/* free result set */
    
mysqli_free_result($result);
}

/* If we have to retrieve large amount of data we use MYSQLI_USE_RESULT */
if ($result mysqli_query($link"SELECT * FROM City"MYSQLI_USE_RESULT)) {

    
/* Note, that we can't execute any functions which interact with the
       server until result set was closed. All calls will return an 
       'out of sync' error */
    
if (!mysqli_query($link"SET @a:='this will not work'")) {
        
printf("Error: %s\n"mysqli_error($link));
    }
    
mysqli_free_result($result);
}

mysqli_close($link);
?>

El resultado del ejemplo seria:

Table myCity successfully created.
Select returned 10 rows.
Error: Commands out of sync;  You can't run this command now