PHP mysqli query() Function
Example - Object Oriented style
Perform query against a database:
<?php
$mysqli = new mysqli("localhost","my_user","my_password","my_db");
// Check connection
if ($mysqli -> connect_errno) {
echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
exit();
}
// Perform query
if ($result = $mysqli -> query("SELECT * FROM Persons"))
{
echo "Returned rows are: " . $result -> num_rows;
// Free result set
$result -> free_result();
}
$mysqli -> close();
?>
Look at example of procedural style at the bottom.
Definition and Usage
The query() / mysqli_query() function performs a query against a database.
Syntax
Object oriented style:
$mysqli -> query(query,
resultmode)
Procedural style:
mysqli_query(connection, query, resultmode)
Parameter Values
Parameter | Description |
---|---|
connection | Required. Specifies the MySQL connection to use |
query | Required. Specifies the SQL query string |
resultmode |
Optional. A constant. Can be one of the following:
|
Technical Details
Return Value: | 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 |
---|---|
PHP Version: | 5+ |
PHP Changelog: | PHP 5.3.0 added the ability for async queries |
Example - Procedural style
Perform query against a database:
<?php
$con = mysqli_connect("localhost","my_user","my_password","my_db");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit();
}
// Perform query
if ($result = mysqli_query($con, "SELECT * FROM Persons"))
{
echo "Returned rows are: " . mysqli_num_rows($result);
// Free result set
mysqli_free_result($result);
}
mysqli_close($con);
?>
❮ PHP MySQLi Reference
Copyright 1999-2023 by Refsnes Data. All Rights Reserved.