我有一个简单的函数来从我的数据库删除帐户。我写的如下
public function removeAccount($email) {
$response = array('code' => 0, 'error' => false);
$stmt = $this->conn->prepare("SELECT id FROM user WHERE email = ?");
$stmt->bind_param("s", $email);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows) {
$user = $result->fetch_assoc();
$id = $user['id'];
$stmt->close();
$stmt = $this->conn->prepare("DELETE FROM number_list WHERE user_id = ?");
$stmt->bind_param("i", $id);
$stmt->execute();
$stmt->close();
$stmt = $this->conn->prepare("DELETE FROM number_status WHERE user_id = ?");
$stmt->bind_param("i", $id);
$stmt->execute();
$stmt->close();
$stmt = $this->conn->prepare("INSERT INTO old_user(email,serial,premium) SELECT email, device_id, membership FROM user WHERE id = ?");
$stmt->bind_param("i", $id);
$stmt->execute();
$stmt->close();
$stmt = $this->conn->prepare("DELETE FROM user WHERE id = ?");
$stmt->bind_param("i", $id);
$stmt->execute();
$stmt->close();
if ($stmt->affected_rows) {
$response["code"] = 1;
}
}
return $response;
}
它在下面给我警告
if ($stmt->affected_rows) {
我已经搜索了解决它的方法,但没有得到什么错误的想法和什么可以解决它。如果有人能够了解它,请检查并告诉我。非常感谢。
$stmt = $this->conn->prepare("DELETE FROM user WHERE id = ?");
$stmt->bind_param("i", $id);
$stmt->execute();
if($stmt->affected_rows > 0) { $response["code"] = 1; }
$stmt->close();
在这种情况下,我们检查是否有任何行更新。作为参考,这里是mysqli::$affected_rows
返回值的用法。
-1 - 查询返回错误;如果execute()
已经有错误处理,那就多余了
0 - 没有在UPDATE
上更新记录,没有与WHERE
子句匹配的行或者没有执行任何查询
大于0 - 返回受影响的行数;与mysqli_result::$num_rows
相比SELECT
您正在尝试从已关闭的语句中获取受影响的行数。代替
$stmt->execute();
$stmt->close();
if ($stmt->affected_rows) {
$response["code"] = 1;
}
使用
$stmt->execute();
$num_affected_rows = $stmt->affected_rows;
$stmt->close();
if ($num_affected_rows) {
$response["code"] = 1;
}