PHP | mysqli_real_query() Function

PHP mysqli_real_query() Function


Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP mysqli_real_query() Function.

PHP mysqli_real_query() function accepts a string value representing a query as one of the parameters and, executes/performs the given query in the database. The passed data in the query should be well escaped.

Syntax

Following below is the syntax to use this function -

mysqli_real_query($con, $query)


Parameter Details

Sr.NoParameter & 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 well escaped.


Return Value

This built-in PHP function returns true on success and false on failure.

PHP Version

This PHP function was first introduced in PHP version 5 and works in all the later versions.

Example1

The following example shows the usage of the PHP mysqli_real_query() function (in a 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, 'Kennedy', 'Nigeria', 'PortHarcourt', 'Nigeria')");
   mysqli_real_query($con, "insert into my_team values(2, 'Jonathan', 'Trott', 'CapeTown', 'SouthAfrica')");
   mysqli_real_query($con, "insert into my_team values(3, 'Queen', 'Dauglas', 'Texas', 'UnitedStates')");
   mysqli_real_query($con, "insert into my_team values(4, 'Paul', 'Francis', 'Texas', 'UnitedStates')");

   print("Records Inserted ..."."\n");

   //Closing the connection
   mysqli_close($con);
?>

Output

When the above code is executed, it will produce the following result -

Table Created...
Record Inserted...

Example2

In object oriented style the syntax of this built-in PHP function is $con->real_query(); The following is the example of this PHP 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('Kennedy', 'Nkpara', 'Nigeria')");
   $con->real_query("insert into players values('Jonathan', 'Trott', 'SouthAfrica')");

   print("Data Created......");
   //Closing the connection
   $res = $con -> close();
?>

Output

When the above code is executed, it will produce the following result -

Data Created......


Alright guys! This is where we are going to be rounding up for this tutorial post. In our next tutorial, we are going to be discussing about the PHP mysqli_refresh() Function.

Do feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Do follow us on our various social media handles available and also subscribe to our newsletter to get our tutorial posts delivered directly to your emails.

Thanks for reading and bye for now.