mysqli_refresh()函数刷新表或缓存,或者重置复制服务器信息。
mysqli_refresh()函数刷新表,刷新日志,刷新缓存。
mysqli_refresh($con, options);
序号 | 参数及说明 |
---|---|
1 | con(必需) 这是一个表示与MySQL Server的连接的对象。 |
2 | options(必需) 这表示MYSQL刷新命令的选项,您可以通过用逗号分隔它们来指定多个选项。
|
PHP mysqli_refresh()函数返回一个布尔值,如果刷新操作成功,则为true,否则为false。
此函数最初是在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 //建立连接 $con = mysqli_connect("localhost", "root", "password", "mydb"); //将自动提交设置为true mysqli_autocommit($con, False); //将记录插入到my_team表中 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_close($con); ?>
因为我们关闭了自动提交选项,所以添加的记录不会保存在数据库中,如果您在MySQL中验证表的内容,它将是空的,如下所示:
mysql> select * from my_team; Empty set (0.00 sec)
您可以使用mysqli_query函数将记录刷新到表中,如下所示:
<?php //建立连接 $con = mysqli_connect("localhost", "root", "password", "mydb"); //将自动提交设置为true mysqli_autocommit($con, False); //将记录插入到my_team表中 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); //关闭连接 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();。以下是面向对象风格中此函数的示例;
<?php $con = new mysqli("localhost", "root", "password", "mydb"); //在players表中插入一个记录 $con->query("CREATE TABLE IF NOT EXISTS players(First_Name VARCHAR(255), Last_Name VARCHAR(255), Country VARCHAR(255))"); //将自动提交设置为false $con->autocommit(FALSE); $con->query("insert into players values('Shikhar', 'Dhawan', 'India')"); $con->query("insert into players values('Jonathan', 'Trott', 'SouthAfrica')"); //正在刷新表 $con->refresh(MYSQLI_REFRESH_TABLES); //关闭连接 $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)
mysqli_refresh使用示例
<?php $connection_mysql = mysqli_connect("localhost","username","password","db"); if (mysqli_connect_errno($connection_mysql)){ echo "连接MySQL失败: " . mysqli_connect_error(); } mysqli_refresh($connection_mysql,MYSQLI_REFRESH_LOG); mysqli_close($connection_mysql); ?>