当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


PHP mysqli_refresh()用法及代码示例



定义和用法

这个mysqli_refresh()函数刷新表,刷新日志,刷新缓存。

用法

mysqli_refresh($con, options);

参数

Sr.No 参数及说明
1

con(Mandatory)

这是一个表示与 MySQL 服务器的连接的对象。

2

options(Mandatory)

这代表了MYSQL刷新命令的选项,可以通过逗号分隔来指定多个选项。

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

由于我们关闭了 auto-commit 选项,添加的记录将不会保存在数据库中,如果您在 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 mysqli_refresh() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。