English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The mysqli_query() function executes a query on the database
mysqli_query()The function accepts a string value representing the query as one of its parameters and executes the given query on the database.
mysqli_query($con, query)
Serial number | Parameters and descriptions |
---|---|
1 | con (required) This is an object representing the connection to the MySQL Server. |
2 | query (required) This is a string value representing the query to be executed. |
3 | mode (optional) This is an integer value representing the result mode. You can useMYSQLI_USE_RESULTorMYSQLI_STORE_RESULTis passed as a value to this parameter. |
failed, it returns FALSE. A successful execution of SELECT, SHOW, DESCRIBE, or EXPLAIN queries using mysqli_query() returns a mysqli_result object, other queries return TRUE.
For other queries, this function returns a boolean value, if the operation/query successful, thentrue,otherwisefalse.
This function was originally introduced in PHP version5introduced and can be used in all higher versions.
The following example demonstratesmysqli_query()Function usage (procedural style)-
<?php $con = mysqli_connect("localhost", "root", "password", "mydb"); mysqli_query($con, "CREATE TABLE IF NOT EXISTS my_team(ID INT, First_Name VARCHAR(255), Last_Name VARCHAR(255), Place_Of_Birth VARCHAR(255), Country VARCHAR(255))"); print("Create table ..."."\n"); //Insert the record into the my_team table mysqli_query($con, "insert into my_team values(1, 'Shikhar', 'Dhawan', 'Delhi', 'India")"); mysqli_query($con, "insert into my_team values(2, 'Jonathan', 'Trott', 'CapeTown', 'SouthAfrica')"); mysqli_query($con, "insert into my_team values(3, 'Kumara', 'Sangakkara', 'Matale', 'Sri Lanka')"); mysqli_query($con, "insert into my_team values(4, 'Virat', 'Kohli', 'Delhi', 'India')"); print("Insert record ..."."\n"); //Close Connection mysqli_close($con); ?>
Output Result
Create table... Insert record ...
If you observe the content of the table in the database, you can see the inserted records as follows:
mysql> select * from my_team; +------+------------+------------+----------------+-------------+ | ID | First_Name | Last_Name | Place_Of_Birth | Country | +------+------------+------------+----------------+-------------+ | 1 | Shikhar | Dhawan | Delhi | India | | 2 | Jonathan | Trott | Cape Town | South Africa | | 3 | Kumara | Sangakkara | Matale | Sri Lanka | | 4 | Virat | Kohli | Delhi | India | +------+------------+------------+----------------+-------------+ 4 rows in set (0.00 sec)
In the object-oriented style, the syntax of this function is}}$con-> query();。The following is an example of this function in an object-oriented style;
<?php $con = new mysqli("localhost", "root", "password", "mydb"); //Insert the record into the players table $con-> query("CREATE TABLE IF NOT EXISTS players(First_Name VARCHAR(255), Last_Name VARCHAR(255), Country VARCHAR(255))"); $con-> query("insert into players values('Shikhar', 'Dhawan', 'India')"); $con-> query("insert into players values('Jonathan', 'Trott', 'SouthAfrica')"); print("Create data......"); //Close Connection $res = $con -> close(); ?>
Output Result
Create data......
If you observe the content of the table in the database, you can see the inserted records as follows:
mysql> select * from players; +------------+-----------+-------------+ | First_Name | Last_Name | Country | +------------+-----------+-------------+ | Shikhar | Dhawan | India | | Jonathan | Trott | SouthAfrica | +------------+-----------+-------------+ 2 rows in set (0.00 sec)
The following example prints the results of INSERT and SELECT queries-
<?php //Establish Connection $con = mysqli_connect("localhost", "root", "password", "mydb"); mysqli_query($con, "CREATE TABLE IF NOT EXISTS my_team(ID INT, First_Name VARCHAR(255), Last_Name VARCHAR(255), Place_Of_Birth VARCHAR(255), Country VARCHAR(255))"); print("Create table ..."."\n"); //Insert the record into the my_team table $res = mysqli_query($con, "insert into my_team values("1, 'Shikhar', 'Dhawan', 'Delhi', 'India")"); print("Insert query result: ",$res,"\n"); $res = mysqli_query($con, "insert into my_team values("2, 'Jonathan', 'Trott', 'CapeTown', 'SouthAfrica')"); print("Insert query result: ").$res; $res = mysqli_query($con, "SELECT * FROM my_team"); print("Result of the SELECT query: "); print_r($res); //Close Connection mysqli_close($con); ?>
Output Result
Create table... Insert query result: 1 Insert query result: 1Result of the SELECT query: mysqli_result Object ( [current_field] => 0 [field_count] => 5 [lengths] => [num_rows] => 2 [type] => 0 )
Assuming we have already created a players table and populated it as follows-
CREATE TABLE Players (Name VARCHAR(255), Age INT, Score INT); insert into Players values('Dhavan', 33, 9),('Rohit', 28, 26),('Kohli', 25, 50);
The following example executes queries against a database:
<?php //Establish Connection $con = mysqli_connect("localhost", "root", "password", "mydb"); //Execute Multiple Queries $query = "SELECT * FROM players"; //Search Records $res = mysqli_query($con, $query, MYSQLI_USE_RESULT); if ($res) { while ($row = mysqli_fetch_row($res)) { print("Name: ".$row[0]."\n"); print("Age: ".$row[1]."\n"); } } //Close Connection mysqli_close($con); ?>
Output Result
Name: Dhavan Age: 33 Name: Rohit Age: 28 Name: Kohli Age: 25