mysqli::refresh is a method of the mysqli class, and its definition is as follows:
public mysqli::refresh (int $options): bool
It receives an integer parameter $options , a combination of one or more constants used to control refresh behavior. For example:
MYSQLI_REFRESH_GRANT
MYSQLI_REFRESH_LOG
MYSQLI_REFRESH_TABLES
MYSQLI_REFRESH_HOSTS
MYSQLI_REFRESH_STATUS
MYSQLI_REFRESH_THREADS
These constants can be used in combination by bitwise or ( | ), for example:
MYSQLI_REFRESH_TABLES | MYSQLI_REFRESH_STATUS
MYSQLI_REFRESH_TABLES is a constant used to indicate refreshing the data table cache. When you make changes to the table structure during the code running (such as adding fields, modifying indexes, etc.) and hope that the current connection can recognize these changes immediately, you can call mysqli::refresh(MYSQLI_REFRESH_TABLES) .
This is very useful for programs that run for a long time, or periodically maintain table structures through scripts.
Here is a complete example showing how to use the mysqli::refresh method with the MYSQLI_REFRESH_TABLES constant:
<?php
$mysqli = new mysqli("localhost", "username", "password", "database");
// Check the connection
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
// Refresh the data table cache
if ($mysqli->refresh(MYSQLI_REFRESH_TABLES)) {
echo "Data table refresh successfully。\n";
} else {
echo "Data table refresh failed。\n";
}
$mysqli->close();
In this example, we connect to the local database server, then call the refresh method and pass in MYSQLI_REFRESH_TABLES , and output the prompt after success.
You can also combine multiple refresh options:
$mysqli->refresh(MYSQLI_REFRESH_TABLES | MYSQLI_REFRESH_STATUS);
In some automated deployment processes, scripts may run queries immediately after updating the database structure. In order to avoid errors caused by cached metadata, calling the refresh method can ensure that the latest structure is used.
For example, some SaaS platforms allow users to dynamically add fields or extend table structures. In this system , mysqli::refresh(MYSQLI_REFRESH_TABLES) can be used to ensure that the application layer code recognizes structural changes in a timely manner.
Although we mainly discuss MYSQLI_REFRESH_TABLES in this article, by the way, if you use a script to modify permissions (such as authorized users), you can use MYSQLI_REFRESH_GRANT to refresh the permission table:
$mysqli->refresh(MYSQLI_REFRESH_GRANT);
The refresh method does not change the actual data or structure in the database, it just forces the mysqli client to reload the internal cache.
Not all MySQL server configurations support all types of refresh operations, depending on the server version and settings.
If you pass in an invalid constant combination when calling the refresh method, it may not report an error, but it will not play any role.