English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP mysqli_refresh() Function Usage and Example

PHP MySQLi Reference Manual

The mysqli_refresh() function refreshes the table or cache, or resets the replication server information.

Definition and Usage

mysqli_refresh()The function refreshes the table, refreshes the log, and refreshes the cache.

Syntax

mysqli_refresh($con, options);

Parameter

Serial NumberParameters and Description
1

con (required)

This is an object representing the connection to the MySQL Server.

2

options (required)

These represent the options for the MYSQL refresh command, which 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

The PHP mysqli_refresh() function returns a boolean value, which istrue,otherwisefalse.

PHP version

This function was originally introduced in PHP version5introduced and can be used in all higher versions.

Online Example

The following example demonstratesmysqli_refresh()Function Usage (Procedural Style)-

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 disables the auto-commit option and attempts to insert a record into this table-

<?php
   //Establish connection
   $con = mysqli_connect("localhost", "root", "password", "mydb");
   //Set the auto-commit to true
   mysqli_autocommit($con, False);
   //Insert 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')");
   //Close Connection
   mysqli_close($con);
?>

Because we have disabled the auto-commit option, the added records will not be saved in the database. If you verify the content of the table in MySQL, it will be empty as shown below:

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

You can use the mysqli_query function to refresh the records into the table as follows:

<?php
   //Establish connection
   $con = mysqli_connect("localhost", "root", "password", "mydb");
   //Set the auto-commit to true
   mysqli_autocommit($con, False);
   //Insert 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);
   //Close Connection
   mysqli_close($con);

Now, if you verify the content of the table my_team, 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 | CapeTown | SouthAfrica |
|  3 | Kumara | Sangakkara | Matale | Srilanka |
|  4 | Virat | Kohli | Delhi | India |
+----+------------+------------+----------------+-------------+
4 rows in set (0.00 sec)

Online Example

In an object-oriented style, the syntax of this function is$con->refresh();.Here is an example of this function in an object-oriented style;

<?php
   $con = new mysqli("localhost", "root", "password", "mydb");
   //Insert a record into the players table
   $con-> query("CREATE TABLE IF NOT EXISTS players(First_Name VARCHAR(255), Last_Name VARCHAR(255), Country VARCHAR(255))");
   //Set Auto Commit to False
   $con-> autocommit(FALSE);
   $con-> query("insert into players values('Shikhar', 'Dhawan', 'India')");
   $con-> query("insert into players values('Jonathan', 'Trott', 'SouthAfrica')");
   //Refreshing tables
   $con-> refresh(MYSQLI_REFRESH_TABLES);
   //Close Connection
   $res = $con -> close();
?>

Output Result

Data Created......

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 | South Africa |
+------------+-----------+-------------+
2 rows in set (0.00 sec)

Online Example

mysqli_refresh Usage Example

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

PHP MySQLi Reference Manual