Current Location: Home> Function Categories> mysqli::$thread_id

mysqli::$thread_id

(mysqli_thread_id) Returns the thread ID of the current connection
Name:mysqli::$thread_id
Category:MySQLi
Programming Language:php
One-line Description:Returns the thread ID of the current connection.

Definition and usage

thread_id() / mysqli_thread_id() function returns the thread ID of the current connection. You can then use the kill() function to terminate the connection.

Note: If the connection is disconnected and reconnected, the thread ID will change. Therefore, get the thread ID only if needed.

Example

Example 1 - Object-Oriented Style

Return the thread ID of the current connection and terminate the connection:

 <?php
$mysqli = new mysqli ( "localhost" , "my_user" , "my_password" , "my_db" ) ;

if ( $mysqli -> connect_errno ) {
  echo "Failed to connect to MySQL: " . $mysqli -> connect_error ;
  exit ( ) ;
}

// Get the thread ID
$thread_id = $mysqli -> thread_id ;

// Terminate the connection
$mysqli -> kill ( $thread_id ) ;
?>

Example 2 - Procedural Style

Return the thread ID of the current connection and terminate the connection:

 <?php
$con = mysqli_connect ( "localhost" , "my_user" , "my_password" , "my_db" ) ;

if ( mysqli_connect_errno ( ) ) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error ( ) ;
  exit ;
}

// Get the thread ID
$thread_id = mysqli_thread_id ( $con ) ;

// Terminate the connection
mysqli_kill ( $con , $thread_id ) ;
?>
Similar Functions
Popular Articles