mysqli_refresh()函數(shù)刷新表或緩存,或者重置復(fù)制服務(wù)器信息。
mysqli_refresh()函數(shù)刷新表,刷新日志,刷新緩存。
mysqli_refresh($con, options);
序號 | 參數(shù)及說明 |
---|---|
1 | con(必需) 這是一個表示與MySQL Server的連接的對象。 |
2 | options(必需) 這表示MYSQL刷新命令的選項(xiàng),您可以通過用逗號分隔它們來指定多個選項(xiàng)。
|
PHP mysqli_refresh()函數(shù)返回一個布爾值,如果刷新操作成功,則為true,否則為false。
此函數(shù)最初是在PHP版本5中引入的,并且可以在所有更高版本中使用。
以下示例演示了mysqli_refresh()函數(shù)的用法(面向過程風(fēng)格)-
假設(shè)我們在數(shù)據(jù)庫mydb中創(chuàng)建了一個名為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) );
以下示例關(guān)閉自動提交選項(xiàng),并嘗試將記錄插入此表中-
<?php //建立連接 $con = mysqli_connect("localhost", "root", "password", "mydb"); //將自動提交設(shè)置為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')"); //關(guān)閉連接 mysqli_close($con); ?>
因?yàn)槲覀冴P(guān)閉了自動提交選項(xiàng),所以添加的記錄不會保存在數(shù)據(jù)庫中,如果您在MySQL中驗(yàn)證表的內(nèi)容,它將是空的,如下所示:
mysql> select * from my_team; Empty set (0.00 sec)
您可以使用mysqli_query函數(shù)將記錄刷新到表中,如下所示:
<?php //建立連接 $con = mysqli_connect("localhost", "root", "password", "mydb"); //將自動提交設(shè)置為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); //關(guān)閉連接 mysqli_close($con);
現(xiàn)在,如果您驗(yàn)證表my_team的內(nèi)容,則可以看到插入的記錄,如下所示:
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)
在面向?qū)ο箫L(fēng)格中,此函數(shù)的語法為$con->refresh();。以下是面向?qū)ο箫L(fēng)格中此函數(shù)的示例;
<?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))"); //將自動提交設(shè)置為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); //關(guān)閉連接 $res = $con -> close(); ?>
輸出結(jié)果
Data Created......
如果您觀察數(shù)據(jù)庫中表的內(nèi)容,則可以看到插入的記錄,如下所示:
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); ?>