PHP Tutorial - PHP mysqli_query() Function






Definition

The mysqli_query() function performs a query against the database.

Syntax

PHP mysqli_query() Function has the following syntax.

mysqli_query(connection,query,resultmode);

Parameter

ParameterIs RequiredDescription
connectionRequired.MySQL connection to use
queryRequired.Query string
resultmodeOptional.MYSQLI_STORE_RESULT (default) or MYSQLI_USE_RESULT (for large amount of data)

For successful SELECT, SHOW, DESCRIBE, or EXPLAIN queries it will return a mysqli_result object.

For other successful queries it will return TRUE. FALSE on failure.





Return

Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object.

For other successful queries mysqli_query() will return TRUE.

Example


<?php/*from   w w  w. ja  va 2s  . co  m*/
$con=mysqli_connect("localhost","my_user","my_password","my_db");
if (mysqli_connect_errno($con)){
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

mysqli_query($con,"SELECT * FROM emp");
mysqli_query($con,"INSERT INTO emp (name) VALUES ('PHP')");

mysqli_close($con);
?>