PHP mysqli_thread_id() 函式



定義和用法

mysqli_thread_id() 函式接受一個連線物件並返回給定連線的執行緒 ID。

語法

mysqli_thread_id($con);

引數

序號 引數和描述
1

con(必填)

這是一個表示與 MySQL 伺服器連線的物件。

返回值

此函式返回一個整數值,表示當前連線的執行緒 ID。

PHP 版本

此函式首次引入於 PHP 5 版本,並在所有後續版本中均有效。

示例

以下示例演示了 mysqli_thread_id() 函式(在過程式風格中)的用法 -

<?php
   //Creating the connection
   $con = mysqli_connect("localhost","root","password","test");

   //Id of the current thread
   $id = mysqli_thread_id($con);
   print("Current thread id: ".$id);
?>

這將產生以下結果 -

Current thread id: 55

示例

在面向物件風格中,此函式的語法為 $con->thread_id; 以下是此函式在面向物件風格中的示例 $minus;

<?php
   //Creating the connection
   $con = new mysqli("localhost","root","password","test");

   //Current thread id
   $id = $con->thread_id;

   print("Current thread id: ".$id);
?>

這將產生以下結果 -

Current thread id: 55

示例

以下是此函式的另一個示例,它重試當前執行緒的 ID 並將其終止 $minus;

<?php
   //Creating the connection
   $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(); 以下是此函式在面向物件風格中的示例 $minus;

<?php
   $connection_mysql=mysqli_connect("localhost","root","password","mydb");
   
   if (mysqli_connect_errno($connection_mysql)){
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
   }
   
   $t_id = mysqli_thread_id($connection_mysql);
   
   $res = mysqli_thread_id($connection_mysql,$t_id);
   
   if($res){
	   print("Thread terminated successfully......");
   }
?>

這將產生以下結果 -

Thread terminated successfully......
php_function_reference.htm
廣告