mysqli_thread_safe()函數(shù)返回是否是線程安全的
mysqli_thread_safe()函數(shù)用于告知本數(shù)據(jù)庫(kù)客戶端庫(kù)是否編譯為線程安全的。
mysqli_thread_safe(void);
此函數(shù)不接受任何參數(shù)。
如果客戶端庫(kù)是線程安全的,則此函數(shù)返回布爾值,該值為TRUE,否則為FALSE。
此函數(shù)最初是在PHP版本5中引入的,并且可以在所有更高版本中使用。
以下示例演示了mysqli_thread_safe()函數(shù)的用法(面向過程風(fēng)格)-
<?php
//建立連接
$con = mysqli_connect("localhost","root","password","test");
//線程是否安全
$res = mysqli_thread_safe();
if($res){
print("是線程安全的");
}else{
print("不是線程安全的");
}
?>輸出結(jié)果
是線程安全的
在面向?qū)ο箫L(fēng)格中,此函數(shù)的語(yǔ)法為$con->thread_id; 以下是面向?qū)ο箫L(fēng)格中此函數(shù)的示例;
<?php
//建立連接
$con = new mysqli("localhost","root","password","mydb");
//線程是否安全
$res = $con->thread_safe();
if($res){
print("是線程安全的");
}else{
print("不是線程安全的");
}
?>輸出結(jié)果
是線程安全的
返回當(dāng)前連接的線程 ID,然后殺死連接:
<?php
//建立連接
$con = mysqli_connect("localhost","root","password","test");
if (mysqli_connect_errno($con)){
print("連接MySQL失敗: " . mysqli_connect_error());
}
$res = mysqli_thread_safe();
//當(dāng)前線程的ID
$id = mysqli_thread_id($con);
if($res){
mysqli_kill($con, $id);
}
?>