PHP mysqli_real_query() Function
Definition and Usage
The mysqli_real_query() function accepts a string value representing a query as one of the parameters and, executes/performs the given query on the database. The data passed in the query should be properly escaped.
Syntax
mysqli_real_query($con, $query)
Parameters
| Sr.No | Parameter & Description |
|---|---|
| 1 |
con(Mandatory) This is an object representing a connection to MySQL Server. |
| 2 |
query(Mandatory) This is a string value representing the query to be executed. Data passed to this query should be properly escaped. |
Return Values
This query returns a boolean value which is true incase of success and false incase of failure.
PHP Version
This function was first introduced in PHP Version 5 and works works in all the later versions.
Example
Following example demonstrates the usage of the mysqli_real_query() function (in 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("Table Created ..."."\n");
//Inserting a records into the my_team table
mysqli_real_query($con, "insert into my_team values(1, 'Shikhar', 'Dhawan', 'Delhi', 'India')");
mysqli_real_query($con, "insert into my_team values(2, 'Jonathan', 'Trott', 'CapeTown', 'SouthAfrica')");
mysqli_real_query($con, "insert into my_team values(3, 'Kumara', 'Sangakkara', 'Matale', 'Srilanka')");
mysqli_real_query($con, "insert into my_team values(4, 'Virat', 'Kohli', 'Delhi', 'India')");
print("Records Inserted ..."."\n");
//Closing the connection
mysqli_close($con);
?>
This will produce following result −
Table Created ... Records Inserted ..
Example
In object oriented style the syntax of this function is $con->real_query(); Following is the example of this function in object oriented style $minus;
<?php
$con = new mysqli("localhost", "root", "password", "mydb");
//Inserting a records into the players table
$con->query("CREATE TABLE IF NOT EXISTS players(First_Name VARCHAR(255), Last_Name VARCHAR(255), Country VARCHAR(255))");
$con->real_query("insert into players values('Shikhar', 'Dhawan', 'India')");
$con->real_query("insert into players values('Jonathan', 'Trott', 'SouthAfrica')");
print("Data Created......");
//Closing the connection
$res = $con -> close();
?>
This will produce following result −
Data Created......