PHP mysqli_kill() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
MySQLi

Definition

In PHP, mysqli_kill() asks the MySQL server to terminate a thread (connection) identified by a process id, returning true on success and false on failure.

The process id usually comes from mysqli_thread_id() on a connection you want to stop. In PHP 8.4+ mysqli_kill() is deprecated; prefer running the SQL KILL command instead.

Syntax

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

PHP
$ok = mysqli_kill($mysqli, $threadId);
CallWhat it does
mysqli_kill($mysqli, mysqli_thread_id($other))Tries to kill another connection's thread id.
mysqli_kill($mysqli, 12345)Asks the server to terminate process id 12345.
if (!mysqli_kill($mysqli, $id)) { echo mysqli_error($mysqli); }Handles failure with the error string.

Beginners rarely need kill. It is an admin-style tool for stuck queries or abandoned sessions. You need privileges on the server, and killing the wrong id disconnects someone else's session.

Library declarations
PHP
mysqli_kill(mysqli $mysql, int $process_id): bool

Parameters

ParameterDescription
mysqlRequired. A mysqli connection used to send the kill request.
process_idRequired. The MySQL thread id to terminate (from SHOW PROCESSLIST or mysqli_thread_id()).

Return Value

Return ValueDescription
booltrue if the server accepted the kill request, otherwise false.

PHP Examples

Example 1 — Show Thread Id Before Any Kill

Most apps only need to read their own thread id. Killing is optional admin work.

PHP
<?php
$mysqli = mysqli_connect("localhost", "demo_user", "demo_pass", "app_db");

echo "My thread id: ", mysqli_thread_id($mysqli), PHP_EOL;

mysqli_close($mysqli);

How It Works

  1. mysqli_thread_id() returns this connection's process id.
  2. You would pass that number to kill only in special admin tools.
  3. The sample prints 118 as a fixed demo value.
  4. Everyday CRUD apps do not call mysqli_kill().

Example 2 — Deprecated Helper vs SQL KILL

Prefer SQL KILL on modern PHP instead of the deprecated function.

PHP
<?php
$mysqli = mysqli_connect("localhost", "demo_user", "demo_pass", "app_db");
$target = 118;

// Preferred on PHP 8.4+:
$ok = mysqli_query($mysqli, "KILL " . (int) $target);

echo $ok ? "Kill request sent." : "Kill failed.", PHP_EOL;

mysqli_close($mysqli);

How It Works

  1. Cast the id to int so the SQL stays a simple number.
  2. mysqli_query() runs KILL on the server.
  3. This avoids the deprecated mysqli_kill() API.
  4. Only administrators with rights should run this.

Example 3 — Illustrative mysqli_kill() Call

Shows the old function shape. Treat the output as a tutorial sample.

PHP
<?php
$mysqli = mysqli_connect("localhost", "demo_user", "demo_pass", "app_db");

$ok = mysqli_kill($mysqli, 118);
var_dump($ok);

mysqli_close($mysqli);

How It Works

  1. mysqli_kill() sends the terminate request for process 118.
  2. true means the server accepted the request in this sample.
  3. On PHP 8. 4 you will also see a deprecation notice in real runs.
  4. Prefer KILL SQL for new code.

Frequently Asked Questions

It asks MySQL to terminate a server thread by process id and returns true or false.
From mysqli_thread_id() on a connection, or from SHOW PROCESSLIST in an admin tool.
Yes, as of PHP 8.4. Use the SQL KILL command through mysqli_query() instead.
You can target your thread id, but that disconnects you. Usually you kill a different stuck session.
Almost never. It is for database administration, not normal insert/select code.

Next: mysqli_more_results()

mysqli_more_results() returns true when more results are still waiting from a multi-statem.

mysqli_more_results() 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