PHP | mysqli_thread_safe() Function

PHP mysqli_thread_safe() 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_safe() Function.

The built-in mysqli_thread_safe() function in PHP determines whether the underlying client library supports thread safety or not.

Syntax

Following below is the syntax to use this function -

mysqli_thread_safe(void);


Parameter Details

This built-in PHP function does not accept any parameters.

Return Value

This built-in PHP function returns true if the underlying client library is thread safe and, false if not.

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 demonstrates the usage of the built-in PHP mysqli_thread_safe() function (in a procedural style) -

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

   //Thread safe or not
   $res = mysqli_thread_safe();

   if($res){
      print("Is thread safe");
   }else{
      print("Is not thread safe");
   }
?>

Output

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

is thread safe

Example2

In object oriented style the syntax of this built-in PHP function is $con->thread_safe(); The following below is the example of this function in an object oriented style $minus;

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

   //Thread safe or not
   $res = $con->thread_safe();

   if($res){
      print("Is thread safe");
   }else{
      print("Is not thread safe");
   }
?>

Output

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

is thread safe

Example3

Try the following example below -

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

   if (mysqli_connect_errno($con)){
      print("Failed to connect to MySQL: " . mysqli_connect_error());
   }
   
   $res = mysqli_thread_safe();

   //Id of the current thread
   $id = mysqli_thread_id($con);
   
   if($res){
      mysqli_kill($con, $id);
   }
?>


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_use_result() 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.