PHP | mysqli_thread_id() Function

PHP mysqli_thread_id() Function


Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP mysqli_thread_id() Function.

The built-in PHP mysqli_thread_id() function accepts a connection object and returns the thread id of the given connection.

Syntax

Following below is the syntax to use this function -

mysqli_thread_id($con);


Parameter Details

Sr.NoParameter & Description
1

con(Mandatory)

This is an object representing a connection to MySQL Server.


Return Value

It returns an integer value representing the thread id of the given connection.

PHP Version

This PHP function was first introduced in PHP version 5 and works in all the later versions.

Example1

The following below is an example which shows the usage of PHP mysqli_thread_id() function (in a procedural style) -

<?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);
?>

Output

When the above code is executed, it will produce the following result -

Current thread id: 55

Example2

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

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

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

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

Output

When the above code is executed, it will produce the following result -

Current thread id: 55

Example3

Following is another example of the PHP mysqli_thread_id() function. It retries the I'd of the current thread and kills it $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......");
   }
?>

Output

When the above code is executed, it will produce the following result -

Failed.....

Example4

In an object oriented style the syntax of this function is $con->kill(); The following below is an example of this built-in function in an object oriented style $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......");
   }
?>

Output

When the above code is executed, it will produce the following result -

Thread terminated successfully......


Alright guys! This is where we are going to be rounding up for this tutorial post. In our next tutorial, we are going to be discussing about the mysqli_thread_safe() Function in PHP.

Do feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Do follow us on our various social media handles available and also subscribe to our newsletter to get our tutorial posts delivered directly to your emails.

Thanks for reading and bye for now.