
mysqli_thread_safe()函数返回是否是线程安全的
定义和用法
mysqli_thread_safe()函数用于告知本数据库客户端库是否编译为线程安全的。
语法
mysqli_thread_safe(void);
参数
此函数不接受任何参数。
返回值
如果客户端库是线程安全的,则此函数返回布尔值,该值为TRUE,否则为FALSE。
PHP版本
此函数最初是在PHP版本5中引入的,并且可以在所有更高版本中使用。
在线示例
以下示例演示了mysqli_thread_safe()函数的用法(面向过程风格)-
<?php
   //建立连接
   $con = mysqli_connect("localhost","root","password","test");
   //线程是否安全
   $res = mysqli_thread_safe();
   if($res){
      print("是线程安全的");
   }else{
      print("不是线程安全的");
   }
?>输出结果
是线程安全的
在线示例
在面向对象风格中,此函数的语法为$con->thread_id; 以下是面向对象风格中此函数的示例;
<?php
   //建立连接
   $con = new mysqli("localhost","root","password","mydb");
   //线程是否安全
   $res = $con->thread_safe();
   if($res){
      print("是线程安全的");
   }else{
      print("不是线程安全的");
   }
?>输出结果
是线程安全的
在线示例
返回当前连接的线程 ID,然后杀死连接:
<?php
   //建立连接
   $con = mysqli_connect("localhost","root","password","test");
   if (mysqli_connect_errno($con)){
      print("连接MySQL失败: " . mysqli_connect_error());
   }
   
   $res = mysqli_thread_safe();
   //当前线程的ID
   $id = mysqli_thread_id($con);
   
   if($res){
      mysqli_kill($con, $id);
   }
?>