PHP mysqli $thread_id Property
The PHP mysqli::$thread_id / mysqli_thread_id() function returns the thread ID for the current connection which can then be killed using the mysqli_kill() function.
Note: The thread ID is assigned on a connection-by-connection basis. Hence, if the connection is broken and then re-established, a new thread ID will be assigned.
Syntax
//Object-oriented style $mysqli->thread_id; //Procedural style mysqli_thread_id(mysql)
Parameters
mysql |
Required. For procedural style only: Specify a mysqli object returned by mysqli_connect() or mysqli_init(). |
Return Value
Returns the Thread ID for the current connection.
Example: Object-oriented style
The example below shows the usage of mysqli::thread_id property.
<?php //establishing connection to the database $mysqli = new mysqli("localhost", "user", "password", "database"); if ($mysqli->connect_errno) { echo "Failed to connect to MySQL: ". $mysqli->connect_error; exit(); } //determining the thread id $thread_id = $mysqli->thread_id; //kill connection $mysqli->kill($thread_id); //this will produce an error if (!$mysqli->query("CREATE TABLE temp LIKE Employee")) { printf("Error: %s\n", $mysqli->error); exit; } //closing the connection $mysqli->close(); ?>
The output of the above code will be similar to:
Error: MySQL server has gone away
Example: Procedural style
The example below shows the usage of mysqli_thread_id() function.
<?php //establishing connection to the database $mysqli = mysqli_connect("localhost", "user", "password", "database"); if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: ". mysqli_connect_error(); exit(); } //determining the thread id $thread_id = mysqli_thread_id($mysqli); //kill connection mysqli_kill($mysqli, $thread_id); //this will produce an error if (!mysqli_query($mysqli, "CREATE TABLE temp LIKE Employee")) { printf("Error: %s\n", mysqli_error($mysqli)); exit; } //closing the connection mysqli_close($mysqli); ?>
The output of the above code will be similar to:
Error: MySQL server has gone away
❮ PHP MySQLi Reference