PHP mysqli_refresh() Function
Learn how PHP's mysqli_refresh() function flushes MySQL server caches, tables, logs, and privileges. Covers refresh flags, procedural and OOP syntax, and common pitfalls.
Introduction
MySQLi is the PHP extension for talking to MySQL databases. The mysqli_refresh() function lets you ask the server to flush internal resources — caches, table definitions, logs, or the grant tables that hold user privileges. It is the programmatic equivalent of running a FLUSH SQL statement.
This page explains what mysqli_refresh() does, the flags it accepts, how to call it from both procedural and object-oriented code, and the privilege gotcha that trips up most first-time users.
Syntax
mysqli_refresh() takes the open connection and a bitmask of refresh flags, and returns true on success or false on failure.
mysqli_refresh(mysqli $mysql, int $flags): bool| Parameter | Description |
|---|---|
$mysql | A connection link returned by mysqli_connect() or new mysqli(...). |
$flags | One or more MYSQLI_REFRESH_* constants combined with the bitwise OR operator (|). |
In object-oriented style the same call is written $mysqli->refresh($flags).
Privilege required. Flushing is a privileged operation. The MySQL account you connect with needs the
RELOADprivilege (historically theSUPERprivilege). A standard application user will get a "access denied" error andmysqli_refresh()will returnfalse.
Refresh flags
The $flags argument selects what to flush. The most common constants are:
| Flag | What it flushes |
|---|---|
MYSQLI_REFRESH_GRANT | Reloads the grant tables (same as FLUSH PRIVILEGES). |
MYSQLI_REFRESH_LOG | Flushes the error, query, and binary logs (log rotation). |
MYSQLI_REFRESH_TABLES | Flushes all open tables and closes their files (FLUSH TABLES). |
MYSQLI_REFRESH_HOSTS | Clears the internal host cache. |
MYSQLI_REFRESH_STATUS | Resets the session/server status variables. |
MYSQLI_REFRESH_THREADS | Flushes the thread cache. |
MYSQLI_REFRESH_REPLICA | Resets the replica (formerly MYSQLI_REFRESH_SLAVE). |
Combine flags with | to perform several actions in one round-trip:
mysqli_refresh($conn, MYSQLI_REFRESH_TABLES | MYSQLI_REFRESH_LOG);How to use mysqli_refresh()
Open a connection, call mysqli_refresh() with the flag you want, and check the boolean return value. Here is a complete procedural example:
<?php
// Create a connection to the MySQL server
$conn = mysqli_connect("localhost", "username", "password", "database");
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Flush server tables and caches
// Requires SUPER or FLUSH privilege
if (mysqli_refresh($conn, MYSQLI_REFRESH_TABLES)) {
echo "Tables flushed successfully.";
} else {
echo "Refresh failed: " . mysqli_error($conn);
}
// To fetch updated data, re-execute the query
$result = mysqli_query($conn, "SELECT * FROM table");
if (!$result) {
die("Query failed: " . mysqli_error($conn));
}
// Process the results
if ($result) {
while ($row = mysqli_fetch_assoc($result)) {
// Process each row of data
}
mysqli_free_result($result);
}
// Close the connection
mysqli_close($conn);
?>The connection is established with mysqli_connect(). mysqli_refresh() then flushes the open tables, the result is checked, and the data is re-read with mysqli_query() and mysqli_fetch_assoc(). Finally the link is released with mysqli_close().
Object-oriented style
If you prefer the OOP interface, the connection is a mysqli object and refresh() is a method on it:
<?php
$mysqli = new mysqli("localhost", "username", "password", "database");
if ($mysqli->connect_errno) {
die("Connection failed: " . $mysqli->connect_error);
}
if ($mysqli->refresh(MYSQLI_REFRESH_TABLES | MYSQLI_REFRESH_LOG)) {
echo "Tables and logs flushed successfully.";
} else {
echo "Refresh failed: " . $mysqli->error;
}
$mysqli->close();
?>Both styles behave identically; the OOP version simply reads the connection's error and connect_error as object properties instead of through mysqli_error().
Use Cases for the MySQLi Refresh Function
The mysqli_refresh() function is valuable for a variety of scenarios, including:
1. Table Cache Management
The mysqli_refresh() function can be used to flush table caches. This is helpful when table structures have changed and you need the server to reload the table definitions.
2. Log Management
The function can flush general, slow, or binary logs. This is useful for database maintenance and ensuring that log files are properly rotated.
3. Privilege Reloading
Developers can use it to reload grant tables after making changes to user privileges, ensuring that permission updates take effect immediately.
Advantages of the MySQLi Refresh Function
The mysqli_refresh() function offers several advantages for PHP developers:
1. Efficient Cache Management
The function allows developers to clear server caches on demand. This is useful in applications that require immediate updates to table definitions or query caches.
2. Improved Database Maintenance
The function ensures that logs and caches are properly managed, which can lead to better server performance and easier troubleshooting.
3. Immediate Privilege Updates
The function is valuable when managing user permissions. By reloading grant tables, the application can ensure that all users are working with the most up-to-date access rights.
Common pitfalls
- Access denied. The most frequent surprise: a regular app account lacks the
RELOAD/SUPERprivilege, so the call returnsfalse. Always check the return value and readmysqli_error(). - It is not the HTTP "refresh".
mysqli_refresh()has nothing to do with reloading a web page. It flushes server-side MySQL state. To re-fetch data into your script you must run the query again — flushing does not return rows. - Renamed constants.
MYSQLI_REFRESH_SLAVE/MYSQLI_REFRESH_MASTERwere renamed toMYSQLI_REFRESH_REPLICA/MYSQLI_REFRESH_SOURCEin newer MySQL/PHP builds; prefer the new names on modern setups. - Prefer SQL when possible. Most code can simply execute
FLUSH TABLES(orFLUSH PRIVILEGES) throughmysqli_query();mysqli_refresh()is a convenience wrapper around the same flush commands.
Conclusion
The mysqli_refresh() function provides a straightforward way to send FLUSH commands to the MySQL server, helping developers manage resources and reload definitions after structural changes or maintenance tasks. With support for multiple refresh flags and immediate effect on caches, logs, and privileges, it remains a practical tool for database administration in PHP applications.
We hope this guide has clarified how to use the mysqli_refresh() function effectively. By following the steps and best practices outlined here, developers can maintain optimal database performance and security.