MySQLi Tutorial MySQLi References

mysqli kill() Method



The mysqli::kill() / mysqli_kill() function is used to ask the server to kill a MySQL thread specified by the process_id parameter. This value must be retrieved by calling the mysqli_thread_id() function.

To stop a running query use the SQL command KILL QUERY processid.

Syntax

//Object-oriented style
public mysqli::kill(process_id)

//Procedural style
mysqli_kill(mysql, process_id)

Parameters

mysql Required. For procedural style only: Specify a mysqli object returned by mysqli_connect() or mysqli_init().
process_id Required. Specify the thread ID returned from mysqli_thread_id().

Return Value

Returns true on success or false on failure.

Example: Object-oriented style

The example below shows the usage of mysqli::kill() method.

<?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_kill() 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

❮ MySQLi Functions Reference