gpt4 book ai didi

php - mysqli准备语句错误 "MySQL server has gone away"

转载 作者:行者123 更新时间:2023-11-30 21:38:37 25 4
gpt4 key购买 nike

我正在努力实现从过程式到面向对象样式的跳转,所以如果我的代码不整洁或有缺陷,请多多关照 - 这里我通过 jQuery 将几篇文章传递给一个类,以便在用户更新记录时检查一个复选框:

这里是数据库连接

class db {

private $host ;
private $username;
private $password;
private $dbname;

protected function conn()
{
$this->host = "localhost";
$this->username = "root";
$this->password = "";
$this->dbname = "mytest";

$db = new mysqli($this->host, $this->username, $this->password, $this->dbname);

if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}

return $db;

}

}

这是更新类

class updOrders extends db {

public $pid;
public $proc;

public function __construct()
{
$this->pid = isset($_POST['pid']) ? $_POST['pid'] : 0;
$this->proc = isset($_POST['proc']) ? $_POST['proc'] : 1;

// $stmt = $this->conn()->query("UPDATE tblorderhdr SET completed = ".$this->proc." WHERE orderid = ".$this->pid);

$stmt = $this->conn()->prepare("UPDATE tblorderhdr SET completed = ? WHERE orderid = ?");
$stmt->bind_param('ii', $this->proc, $this->pid);

$stmt->execute();

if($stmt->error)
{
$err = $stmt->error ;
} else {
$err = 'ok';
}

/* close statement */
$stmt->close();

echo json_encode($err);

}

}

$test = new updOrders;

当我注释掉 prepare 语句并直接运行查询(注释掉)时,它会更新,当我尝试将其作为 prepare 语句运行时,它会返回错误“MySQL 服务器已消失”。

最佳答案

我查看了您的代码并找到了这一点。

$stmt = $this->conn()->prepare("UPDATE tblorderhdr SET completed = ? WHERE orderid = ?");

我写的差不多。但是我看到你把连接和 prepare 函数分开了。

$db = $this->conn();

$stmt = $db->prepare("UPDATE tblorderhdr SET completed = ? WHERE orderid = ?");

我也不知道为什么,但现在可以了。

关于php - mysqli准备语句错误 "MySQL server has gone away",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52763537/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com