Auto refresh table data in php



Definition and Usage

The mysqli_refresh() function refreshes the tables, flushes the logs, flushes the caches.

Syntax

mysqli_refresh($con, options);

Parameters

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 Values

The PHP mysqli_refresh() function returns a boolean value which is, true if the refresh operation is successful and, false if not.

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_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)

Example

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)

Example

<?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);
?>

php_function_reference.htm

How to refresh table in PHP?

PHP mysqli_refresh() Function The mysqli_refresh() function refreshes the tables, flushes the logs, flushes the caches.

How to refresh data in PHP?

Use header() function to refresh a web page in PHP.

How to refresh a function in PHP?

Answer: Use the header() Function You can simply use the header() function to automatically refresh a page periodically (i.e. at certain time intervals) using PHP. Please, note that header() function must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP script.

How can I retrieve data from database in PHP without refreshing page?

Step 2..
Load Data From Database Without Page Refresh Using Ajax and jQuery..
Load Results From Database On Page Scroll Using jQuery,Ajax And PHP..
Create Load More Results From Database System Using jQuery,Ajax,PHP and MySQL..
Submit The Form Without Page Refresh Using Ajax And jQuery..