mysqli_thread_id()函数返回当前连接的线程 ID
mysqli_thread_id()函数接受一个连接对象,并返回给定连接的线程ID。
mysqli_thread_id($con);
| 序号 | 参数及说明 |
|---|---|
| 1 | con(必需) 这是一个表示与MySQL Server的连接的对象。 |
此函数返回一个整数值,该整数值表示当前连接的线程ID。
此函数最初是在PHP版本5中引入的,并且可以在所有更高版本中使用。
以下示例演示了mysqli_thread_id()函数的用法(面向过程风格)-
<?php
//建立连接
$con = mysqli_connect("localhost","root","password","test");
//当前线程的ID
$id = mysqli_thread_id($con);
print("当前线程的ID: ".$id);
?>输出结果
当前线程的ID: 55
在面向对象风格中,此函数的语法为$con->thread_id; 以下是面向对象风格中此函数的示例;
<?php
//建立连接
$con = new mysqli("localhost","root","password","test");
//当前线程ID
$id = $con->thread_id;
print("当前线程的ID: ".$id);
?>输出结果
当前线程的ID: 55
以下是此函数的另一个示例,返回当前连接的线程 ID,然后使用 mysqli_kill() 函数杀死该连接:
<?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......");
}
?>输出结果
Failed.....
在面向对象风格中,此函数的语法为$con->kill();。以下是面向对象风格中此函数的示例;
<?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("线程已成功终止......");
}
?>输出结果
线程已成功终止......