English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP mysqli_thread_id() Function Usage and Example

PHP MySQLi Reference Manual

The mysqli_thread_id() function returns the current connection's thread ID

Definition and Usage

mysqli_thread_id()The function accepts a connection object and returns the thread ID of the given connection.

Syntax

mysqli_thread_id($con);

Parameter

Serial NumberParameters and Description
1

con(Required)

This is an object representing a connection with the MySQL Server.

Return value

This function returns an integer value that represents the thread ID of the current connection.

PHP version

This function was initially introduced in PHP version5introduced and can be used in all higher versions.

Online Example

The following examples demonstratemysqli_thread_id()Usage of the Function (Procedural Style)-

<?php
   //Establish Connection
   $con = mysqli_connect("localhost", "root", "password", "test");
   //Current Thread ID
   $id = mysqli_thread_id($con);
   print("Current Thread ID: " . $id);
?>

Output Result

Current Thread ID: 55

Online Example

In the object-oriented style, the syntax of this function is$con->thread_id; The following is an example of this function in an object-oriented style;

<?php
   //Establish Connection
   $con = new mysqli("localhost", "root", "password", "test");
   //Current Thread ID
   $id = $con->thread_id;
   print("Current Thread ID: " . $id);
?>

Output Result

Current Thread ID: 55

Online Example

The following is another example of this function, which returns the thread ID of the current connection and then uses the mysqli_kill() function to kill the connection:

<?php
   //Establish 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.....");
   }
      print("Failed......");
   }
?>

Output Result

Failed.....

Online Example

In the object-oriented style, the syntax of this function is$con->kill();。The following is an example of this function in an object-oriented style;

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

Output Result

Thread terminated successfully......

PHP MySQLi Reference Manual