W3docs

kill

In this article, we will focus on the mysqli_kill() function in PHP, which is used to terminate a MySQL client connection. We will provide you with an overview

In this article, we will focus on the mysqli_kill() function in PHP, which asks the MySQL server to terminate a client connection identified by its thread (connection) ID. We will cover its syntax, parameters, both calling styles, and the real situations where killing a connection makes sense.

What mysqli_kill() does

mysqli_kill() is a built-in PHP function that sends a KILL command to the MySQL server for a given thread ID — the numeric identifier MySQL assigns to every client connection. It is the programmatic equivalent of running KILL <id>; in the MySQL shell.

A few points worth understanding before you use it:

  • The thread ID you pass must belong to a connection on the same MySQL server. You typically obtain it with mysqli_thread_id().
  • Killing a connection terminates it on the server side, but the local PHP mysqli object stays in scope. You should still call mysqli_close() to release it cleanly.
  • Your database user needs the SUPER (or CONNECTION_ADMIN) privilege to kill connections it does not own.

When would you use it?

You rarely kill your own connection — closing it with mysqli_close() is simpler. mysqli_kill() earns its place when you have more than one connection and need to terminate a different one:

  • A long-running query on another connection is holding locks and must be stopped.
  • A monitoring or admin script needs to drop a stale or runaway connection by ID.
  • You want to free a specific session after detecting unexpected or unauthorized activity.

Syntax and parameters

mysqli_kill() works in both the procedural and object-oriented MySQLi styles.

// Procedural
mysqli_kill(mysqli $mysql, int $thread_id): bool

// Object-oriented
$mysqli->kill(int $thread_id): bool

Parameters:

  • mysql — A MySQLi connection object (procedural style only).
  • thread_id — The connection/thread ID to terminate.

Return value: Returns true on success, false on failure.

Procedural example

The example below opens a connection, looks up its own thread ID, kills it, and then closes the handle:

<?php
$mysqli = mysqli_connect("localhost", "username", "password", "database");

if (!$mysqli) {
    die("Connection failed: " . mysqli_connect_error());
}

$thread_id = mysqli_thread_id($mysqli);

if (mysqli_kill($mysqli, $thread_id)) {
    echo "Connection {$thread_id} was killed on the server.";
}

mysqli_close($mysqli);
?>

Here we connect with mysqli_connect(), check the result to avoid fatal errors on later calls (see mysqli_connect_error()), read the current thread ID with mysqli_thread_id(), and pass it to mysqli_kill(). After the kill, the server-side connection is gone, so any further mysqli_query() on this handle would fail — we just close it.

Object-oriented example

The same logic in OOP style, killing a second connection from the first — the common real-world pattern:

<?php
$admin  = new mysqli("localhost", "username", "password", "database");
$worker = new mysqli("localhost", "username", "password", "database");

if ($admin->connect_errno || $worker->connect_errno) {
    die("Connection failed.");
}

// Suppose $worker is running something we need to stop.
$worker_id = $worker->thread_id;

if ($admin->kill($worker_id)) {
    echo "Killed worker connection {$worker_id}.";
}

$admin->close();
$worker->close();
?>

Conclusion

mysqli_kill() asks the MySQL server to terminate a connection by its thread ID, returning true on success and false on failure. Its real value shows up when an additional connection must be stopped — to release locks, drop a runaway query, or end a suspicious session — while a plain mysqli_close() remains the right tool for ending a connection you own.

Practice

Practice
Which function is used in PHP to terminate the execution of the scripts?
Which function is used in PHP to terminate the execution of the scripts?
Was this page helpful?