We now have a youtube channel. Subscribe!

PHP | mysqli_refresh() Function

PHP mysqli_refresh() 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_refresh() Function.

PHP mysqli_refresh() function refreshes the table, flushes the logs and the caches.

Syntax

Following below is the syntax to use this function -

mysqli_refresh($con, options);


Parameter Details

Sr.NoParameter & Description
1

con(Mandatory)

This is an object representing a connection to MySQL Server.

2

options(Mandatory)

This represents the options of the MYSQL refresh command, you can specify multiple options by separating them with commas.

  • MYSQLI_REFRESH_GRANT

  • MYSQLI_REFRESH_LOG

  • MYSQLI_REFRESH_TABLES

  • MYSQLI_REFRESH_HOSTS

  • MYSQLI_REFRESH_STATUS

  • MYSQLI_REFRESH_THREADS

  • MYSQLI_REFRESH_SLAVE

  • MYSQLI_REFRESH_MASTER


Return Value

This function returns the boolean value true if the flush operation is successful or else, false on failure.

PHP Version

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

Example1

Assuming we have created a table named my_team in the database mydb, as follows -

CREATE TABLE my_team(
   ID INT PRIMARY KEY AUTO_INCREMENT,
   First_Name VARCHAR(255), 
   Last_Name VARCHAR(255), 
   Place_Of_Birth VARCHAR(255), 
   Country VARCHAR(255)
);

The following example below turns the auto commit option off and then tries to insert records into the table -

<?php
   //Creating a connection
   $con = mysqli_connect("localhost", "root", "password", "mydb");

   //Setting auto commit to true
   mysqli_autocommit($con, False);

   //Inserting a records into the my_team table
   mysqli_query($con, "insert into my_team values(1, 'Kennedy', 'Nkpara', 'PortHarcourt', 'Nigeria')");
   mysqli_query($con, "insert into my_team values(2, 'Jonathan', 'Trott', 'CapeTown', 'SouthAfrica')");
   mysqli_query($con, "insert into my_team values(3, 'Queen', 'Dauglas', 'Texas', 'UnitedStates')");
   mysqli_query($con, "insert into my_team values(4, 'Paul', 'Francis', 'Texas', 'UnitedStates')");

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

Output

Since we have turned off the auto-commit option, the records added will not be saved in the database and, if you verify the table contents in MYSQL, it will be empty as shown below $minus;

mysql> select * from my_team;
Empty set (0.00 sec)

You can flush the records into the table by making use of the mysqli_query() function as shown below $minus;

<?php
   //Creating a connection
   $con = mysqli_connect("localhost", "root", "password", "mydb");
   //Setting auto commit to true
   mysqli_autocommit($con, False);
   //Inserting a records into the my_team table
   mysqli_query($con, "insert into my_team values(1, 'Kennedy', 'Nkpara', 'PortHarcourt', 'Nigeria')");
   mysqli_query($con, "insert into my_team values(2, 'Jonathan', 'Trott', 'CapeTown', 'SouthAfrica')");
   mysqli_query($con, "insert into my_team values(3, 'Queen', 'Dauglas', 'Texas', 'UnitedStates')");
   mysqli_query($con, "insert into my_team values(4, 'Paul', 'Francis', 'Texas', 'UnitedStates')");

   mysqli_refresh($con, MYSQLI_REFRESH_TABLES);

   //Closing the connection
   mysqli_close($con);

Now, if you verify the contents of the table my_team then, you can see the inserted records as shown below -

mysql> select * from my_team;
+----+------------+------------+----------------+-------------+
| ID | First_Name | Last_Name  | Place_Of_Birth | Country     |
+----+------------+------------+----------------+-------------+
|  1 | Kennedy    | Nkpara     | PortHarcourt   | Nigeria     |
|  2 | Jonathan   | Trott      | CapeTown       | SouthAfrica |
|  3 | Queen      | Dauglas    | Texas          | UnitedStates|
|  4 | Paul       | Francis    | Texas          | UnitedStates|
+----+------------+------------+----------------+-------------+
4 rows in set (0.00 sec)

Example2

In object oriented style the syntax of this built-in PHP function is $con->refresh(); Following is the example of this function in an 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))");

   //Turning the auto-commit false
   $con->autocommit(FALSE);

   $con->query("insert into players values('Kennedy', 'Nkpara', 'Nigeria')");
   $con->query("insert into players values('Jonathan', 'Trott', 'SouthAfrica')");

   //refreshing the table
   $con->refresh(MYSQLI_REFRESH_TABLES);

   //Closing the connection
   $res = $con -> close();
?>

Output

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

Data Created......

If we observe the contents of the table in the database, you can see the inserted records as shown below -

mysql> select * from players;
+------------+-----------+-------------+
| First_Name | Last_Name | Country     |
+------------+-----------+-------------+
| Kennedy    | Nkpara    | Nigeria     |
| Jonathan   | Trott     | SouthAfrica |
+------------+-----------+-------------+
2 rows in set (0.00 sec)

Example3

Try the following example below -

<?php
  $connection_mysql = mysqli_connect("localhost","username","password","db");
   
   if (mysqli_connect_errno($connection_mysql)){
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
   }   
   mysqli_refresh($connection_mysql,MYSQLI_REFRESH_LOG);
   mysqli_close($connection_mysql);
?>


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_rollback() 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.

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.
© 2023 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain