PHP Tutorial - PHP MySQL mysqli_thread_id() Function






Definition

The mysqli_thread_id() function returns the current connection thread ID. After we have the id we can kill the connection with the mysqli_kill() function.

Syntax

PHP MySQL mysqli_thread_id() Function has the following syntax.

mysqli_thread_id(connection);

Parameter

  • connection - Procedural style only: A link identifier returned by mysqli_connect() or mysqli_init()

Return

Returns the Thread ID for the current connection.





Example 1

Returns the current connection thread ID


<?php
    $con=mysqli_connect("localhost","my_user","my_password","my_db");
    if (mysqli_connect_errno($con)){
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    $t_id=mysqli_thread_id($con);
    
    mysqli_kill($con,$t_id);
?>

The thread ID is changed each time we reconnect.





Example 2


<?php// www . ja  v  a2  s  .c  om
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$thread_id = $mysqli->thread_id;

$mysqli->kill($thread_id);

// This should produce an error
if (!$mysqli->query("select * from emp")) {
    printf("Error: %s\n", $mysqli->error);
    exit;
}


$mysqli->close();
?>