mysqli_ping()函數(shù)進(jìn)行一個(gè)服務(wù)器連接,如果連接已斷開則嘗試重新連接。
mysqli_ping()函數(shù)接受連接對(duì)象作為參數(shù),驗(yàn)證連接,如果連接斷開,它將重新連接到服務(wù)器。
mysqli_ping($con,[$host, $username, $passwd, $dname, $port, $socket, $flags] )
序號(hào) | 參數(shù)及說(shuō)明 |
---|---|
1 | con(可選) 這是一個(gè)表示與MySQL Server的連接的對(duì)象。 |
此函數(shù)返回布爾值,如果操作成功,則返回true;如果操作失敗,則返回false。
此函數(shù)最初是在PHP版本5中引入的,并且可以在所有更高版本中使用。
以下示例演示了mysqli_ping()函數(shù)的用法(面向過(guò)程風(fēng)格),檢測(cè)服務(wù)器連接:
<?php //建立連接 $con = mysqli_connect("localhost","root","password","test"); $res = mysqli_ping($con); if($res){ print("Successful....."); }else{ print("Failed......"); } ?>
輸出結(jié)果
Successful.....
在面向?qū)ο箫L(fēng)格中,此函數(shù)的語(yǔ)法為$con-> ping();。以下是面向?qū)ο箫L(fēng)格中此函數(shù)檢測(cè)服務(wù)器連接的示例;
<?php //建立連接 $con = new mysqli("localhost","root","password","test"); $res = $con->ping(); if($res){ print("Successful....."); }else{ print("Failed......"); } ?>
輸出結(jié)果
Successful.....
在面向?qū)ο箫L(fēng)格中,此函數(shù)的語(yǔ)法為$con-> ping();。以下是面向?qū)ο箫L(fēng)格中此函數(shù)的示例;
<?php $connection_mysql = mysqli_connect("localhost","root","password","mydb"); if (mysqli_connect_errno($connection_mysql)){ echo "連接MySQL失敗: " . mysqli_connect_error(); } if (mysqli_ping($connection_mysql)){ echo "Connection is ok!"."\n"; }else{ echo "Error: ". mysqli_error($connection_mysql); } mysqli_close($connection_mysql); ?>
輸出結(jié)果
Connection is ok! Connection was successful