mysqli_thread_id()函數(shù)返回當(dāng)前連接的線程 ID
mysqli_thread_id()函數(shù)接受一個連接對象,并返回給定連接的線程ID。
mysqli_thread_id($con);
| 序號 | 參數(shù)及說明 |
|---|---|
| 1 | con(必需) 這是一個表示與MySQL Server的連接的對象。 |
此函數(shù)返回一個整數(shù)值,該整數(shù)值表示當(dāng)前連接的線程ID。
此函數(shù)最初是在PHP版本5中引入的,并且可以在所有更高版本中使用。
以下示例演示了mysqli_thread_id()函數(shù)的用法(面向過程風(fēng)格)-
<?php
//建立連接
$con = mysqli_connect("localhost","root","password","test");
//當(dāng)前線程的ID
$id = mysqli_thread_id($con);
print("當(dāng)前線程的ID: ".$id);
?>輸出結(jié)果
當(dāng)前線程的ID: 55
在面向?qū)ο箫L(fēng)格中,此函數(shù)的語法為$con->thread_id; 以下是面向?qū)ο箫L(fēng)格中此函數(shù)的示例;
<?php
//建立連接
$con = new mysqli("localhost","root","password","test");
//當(dāng)前線程ID
$id = $con->thread_id;
print("當(dāng)前線程的ID: ".$id);
?>輸出結(jié)果
當(dāng)前線程的ID: 55
以下是此函數(shù)的另一個示例,返回當(dāng)前連接的線程 ID,然后使用 mysqli_kill() 函數(shù)殺死該連接:
<?php
//建立連接
$con = mysqli_connect("localhost","root","password","test");
$id = mysqli_thread_id($con);
mysqli_kill($con, $id);
$res = mysqli_query($con, "CREATE TABLE Sample (name VARCHAR(255))");
if($res){
print("Successful.....");
}else{
print("Failed......");
}
?>輸出結(jié)果
Failed.....
在面向?qū)ο箫L(fēng)格中,此函數(shù)的語法為$con->kill();。以下是面向?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();
}
$t_id = mysqli_thread_id($connection_mysql);
$res = mysqli_thread_id($connection_mysql,$t_id);
if($res){
print("線程已成功終止......");
}
?>輸出結(jié)果
線程已成功終止......