PHP mysqli_refresh() Function

The mysqli_refresh() function refreshes the tables, flushes the logs, flushes the caches.
mysqli_refresh($con, options);

con(Mandatory)
This is an object representing a connection to MySQL Server.
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_GRANT
MYSQLI_REFRESH_LOG
MYSQLI_REFRESH_LOG
MYSQLI_REFRESH_TABLES
MYSQLI_REFRESH_TABLES
MYSQLI_REFRESH_HOSTS
MYSQLI_REFRESH_HOSTS
MYSQLI_REFRESH_STATUS
MYSQLI_REFRESH_STATUS
MYSQLI_REFRESH_THREADS
MYSQLI_REFRESH_THREADS
MYSQLI_REFRESH_SLAVE
MYSQLI_REFRESH_SLAVE
MYSQLI_REFRESH_MASTER
MYSQLI_REFRESH_MASTER
The PHP mysqli_refresh() function returns a boolean value which is, true if the refresh operation is successful and, false if not.
This function was first introduced in PHP Version 5 and works works in all the later versions.
Following example demonstrates the usage of the mysqli_refresh() function (in procedural style) −
Assume 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)
);
Following example turn the auto commit option off and tries to insert records into this 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, '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', 'Srilanka')");
   mysqli_query($con, "insert into my_team values(4, 'Virat', 'Kohli', 'Delhi', 'India')");

   //Closing the connection
   mysqli_close($con);
?>
Since we have turned the auto-commit option off, The records add will not be saved in the database and, if you verify the contents of the table 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 using 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, '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', 'Srilanka')");
   mysqli_query($con, "insert into my_team values(4, 'Virat', 'Kohli', 'Delhi', 'India')");

   mysqli_refresh($con, MYSQLI_REFRESH_TABLES);

   //Closing the connection
   mysqli_close($con);
Now, if you and 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 | Shikhar    | Dhawan     | Delhi          | India       |
|  2 | Jonathan   | Trott      | CapeTown       | SouthAfrica |
|  3 | Kumara     | Sangakkara | Matale         | Srilanka    |
|  4 | Virat      | Kohli      | Delhi          | India       |
+----+------------+------------+----------------+-------------+
4 rows in set (0.00 sec)

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

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

   $con->query("insert into players values('Shikhar', 'Dhawan', 'India')");
   $con->query("insert into players values('Jonathan', 'Trott', 'SouthAfrica')");

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

   //Closing the connection
   $res = $con -> close();
?>
This will produce following result −
Data Created......

If you 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     |
+------------+-----------+-------------+
| Shikhar    | Dhawan    | India       |
| Jonathan   | Trott     | SouthAfrica |
+------------+-----------+-------------+
2 rows in set (0.00 sec)
<?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);
?>

    45 Lectures 
     9 hours 

 Malhar Lathkar

    34 Lectures 
     4 hours 

 Syed Raza

    84 Lectures 
     5.5 hours 

 Frahaan Hussain

    17 Lectures 
     1 hours 

 Nivedita Jain

    100 Lectures 
     34 hours 

 Azaz Patel

    43 Lectures 
     5.5 hours 

 Vijay Kumar Parvatha Reddy
 Print
 Add Notes
 Bookmark this page