PHP mysqli_query() Function is used to execute mysql query on database. A mysqli_query() function is PHP built-in function.
The MySQLi Extension is a relational database driver used in the PHP scripting language to provide an interface with MySQL databases.
A mysqli_query() Function performs SELECT, INSERT, UPDATE or DELETE etc. mysql query.
PHP Version : PHP 5, PHP 7, PHP 8
Syntax for mysqli_query:
mysqli_query(mysqli $connection, string $query, int $result_mode= MYSQLI_STORE_RESULT);
Parameter,
$connection : It is MySQL database connection.
$query : It is mysql query in string.
$result_mode : Optional. They have two constant values as below.
1. MYSQLI_STORE_RESULT -This is default value.
2. MYSQLI_USE_RESULT - Use this to get large amount of data.
Return values for mysqli_query:
A mysqli_query() Function returns result set for SELECT, SHOW, DESCRIBE, or EXPLAIN queries. For other successful queries, It will return TRUE otherwise it returns FALSE on failure.
Example :
<?php
$connection = mysqli_connect("localhost","username","password","database_name");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL server : " . mysqli_connect_error();
exit();
}
// Perform mysql select query.
if ($resultset = mysqli_query($connection , "SELECT * FROM employee")) {
echo "Returned data are: " . mysqli_num_rows($resultset);
// Free result set
mysqli_free_result($result);
}
mysqli_close($connection );
?>
Comments