PHP mysqli_thread_id() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
MySQLi

Definition

In PHP, mysqli_thread_id() returns the connection’s thread ID as assigned by the MySQL server.

That ID identifies the session on the server. Advanced tools may pass it to mysqli_kill() to ask the server to terminate that thread. Everyday CRUD code rarely needs it.

Syntax

Call mysqli_thread_id() with the values you need. Start with this line.

PHP
$id = mysqli_thread_id($mysql);
CallWhat it does
mysqli_thread_id($link)Returns the integer thread id for this connection.
$mysqli->thread_idObject-oriented property form.
mysqli_kill($link, mysqli_thread_id($link))Advanced pattern: request killing that server thread (use with care).

In OO style, thread_id is typically read as a property. The value is per connection and can change if you reconnect. Do not hard-code thread IDs.

Library declarations
PHP
mysqli_thread_id(mysqli $mysql): int

Parameters

ParameterDescription
mysqlRequired. Procedural style only: an open mysqli connection.

Return Value

Return ValueDescription
intThe MySQL thread ID for the connection.

PHP Examples

Example 1 — Read the Thread ID

Fixed demo printing a sample thread id.

PHP
<?php
$threadId = 42; // stand-in for mysqli_thread_id($link)
echo 'thread_id=', $threadId, PHP_EOL;

How It Works

  1. Connect to MySQL.
  2. Call mysqli_thread_id($link) or read $mysqli->thread_id.
  3. Print or log the integer if you need it.
  4. Sample output when connected to a demo database shows the real session id.

Example 2 — ID Changes After Reconnect

Show that a new connection gets a new id.

PHP
<?php
echo 'first connection thread_id=42', PHP_EOL;
echo 'reconnect...', PHP_EOL;
echo 'second connection thread_id=77', PHP_EOL;

How It Works

  1. Thread IDs belong to a live server session.
  2. Closing and opening again usually yields a new id.
  3. Never cache an old id across reconnects.
  4. Read thread_id again after you reconnect.

Example 3 — OO Property Access

Object style reads a property rather than calling a method in common code.

PHP
<?php
class DemoMysqli {
    public $thread_id = 99;
}
$mysqli = new DemoMysqli();
echo $mysqli->thread_id, PHP_EOL;

How It Works

  1. OO tutorials often show $mysqli->thread_id.
  2. Procedural code uses mysqli_thread_id($link).
  3. Both expose the same server-assigned integer.
  4. Use it for diagnostics or careful admin kill tooling only.

Frequently Asked Questions

The MySQL server thread ID for the current connection as an integer.
Rarely. Mostly for diagnostics or advanced session control with mysqli_kill().
You usually read the $mysqli->thread_id property; procedural code calls mysqli_thread_id($link).
No. It is for the live session and changes when you open a new connection.
Almost never. Closing your connection is the normal way to end your own session.

Next: mysqli_thread_safe()

mysqli_thread_safe() reports whether the MySQLi client library was built thread-safe.

mysqli_thread_safe() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful