PHP mysqli_refresh() 函数



定义和用法

mysqli_refresh() 函数刷新表、刷新日志、刷新缓存。

语法

mysqli_refresh($con, options);

参数

序号 参数及描述
1

con(必填)

表示与 MySQL 服务器的连接对象。

2

options(必填)

表示 MYSQL refresh 命令的选项,可以使用逗号分隔多个选项。

  • MYSQLI_REFRESH_GRANT

  • MYSQLI_REFRESH_LOG

  • MYSQLI_REFRESH_TABLES

  • MYSQLI_REFRESH_HOSTS

  • MYSQLI_REFRESH_STATUS

  • MYSQLI_REFRESH_THREADS

  • MYSQLI_REFRESH_SLAVE

  • MYSQLI_REFRESH_MASTER

返回值

PHP mysqli_refresh() 函数返回一个布尔值,如果刷新操作成功则返回 true,否则返回 false

PHP 版本

此函数首次引入于 PHP 5 版本,并在所有后续版本中有效。

示例

以下示例演示了 mysqli_refresh() 函数的使用(过程式风格) -

假设我们在数据库 mydb 中创建了一个名为 my_team 的表,如下所示 -

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

以下示例关闭自动提交选项,并尝试向此表中插入记录 -

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

由于我们已关闭自动提交选项,因此添加的记录不会保存到数据库中,并且如果您在 MySQL 中验证表的内容,则它将为空,如下所示 $minus;

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

您可以使用 mysqli_query 函数将记录刷新到表中,如下所示 $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);

现在,如果您验证 my_team 表的内容,则可以看到插入的记录,如下所示 -

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)

示例

在面向对象风格中,此函数的语法为 $con->refresh(); 以下是此函数在面向对象风格中的示例 $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();
?>

这将产生以下结果 -

Data Created......

如果您观察数据库中表的的内容,则可以看到插入的记录,如下所示 -

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);
?>
php_function_reference.htm
广告