mysqli::kill

mysqli_kill

(PHP 5, PHP 7, PHP 8)

mysqli::kill -- mysqli_kill让服务器杀死一个 MySQL 线程

说明

面向对象风格

public mysqli::kill(int $process_id): bool

过程化风格

mysqli_kill(mysqli $mysql, int $process_id): bool

本函数可以用来让服务器杀掉 process_id 参数指定的 MySQL 线程。必须通过调用 mysqli_thread_id() 函数来检索该值。

要停止正在运行的查询,应该使用 SQL 命令 KILL QUERY processid

参数

mysql

仅以过程化样式:由 mysqli_connect()mysqli_init() 返回的 mysqli 对象。

返回值

成功时返回 true, 或者在失败时返回 false

错误/异常

If mysqli error reporting is enabled (MYSQLI_REPORT_ERROR) and the requested operation fails, a warning is generated. If, in addition, the mode is set to MYSQLI_REPORT_STRICT, a mysqli_sql_exception is thrown instead.

示例

示例 #1 mysqli::kill() 示例

面向对象风格

<?php
$mysqli
= new mysqli("localhost", "my_user", "my_password", "world");

/* 检查连接是否成功 */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}

/* 获得连接对应的线程 ID */
$thread_id = $mysqli->thread_id;

/* 杀掉连接 */
$mysqli->kill($thread_id);

/* 下面的代码应该会发生错误 */
if (!$mysqli->query("CREATE TABLE myCity LIKE City")) {
printf("Error: %s\n", $mysqli->error);
exit;
}

/* 关闭连接 */
$mysqli->close();
?>

过程化风格

<?php
$link
= mysqli_connect("localhost", "my_user", "my_password", "world");

/* 检查连接是否成功 */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}

/* 获得连接对应的线程 ID */
$thread_id = mysqli_thread_id($link);

/* 杀掉连接 */
mysqli_kill($link, $thread_id);

/* 下面的代码应该会发生错误 */
if (!mysqli_query($link, "CREATE TABLE myCity LIKE City")) {
printf("Error: %s\n", mysqli_error($link));
exit;
}

/* 关闭连接 */
mysqli_close($link);
?>

以上示例会输出:

Error: MySQL server has gone away

参见

add a note add a note

User Contributed Notes 1 note

up
1
johnjawed at gmail dot com
19 years ago
Be careful using this before mysqli::close.

Killing the thread before actually closing the connection will leave the connection open! And depending on your max_connections and max_user_connections (by default the same), this could result in a "Max connections reached for **** user" message.
To Top