mysqli 准备语句错误“MySQL 服务器已消失" [英] mysqli prepare statement error "MySQL server has gone away"
问题描述
我正在努力制作程序到面向对象样式的跳转形式,所以如果我的代码不整洁或有缺陷,请保持友好 - 在这里,我通过 jQuery 将一些帖子传递给一个类,以便在用户使用时更新记录选中一个复选框:
I'm struggling to make the jump form Procedural to Object Orientated style so if my code is untidy or flawed please be nice - here I'm passing a couple of posts via jQuery to a class to update a record when the user checks a checkbox:
这里是数据库连接
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;
当我注释掉准备语句并直接运行查询(注释掉)时,它会更新,当我尝试将它作为准备语句运行时,它返回错误MySQL 服务器已消失".
When I comment out the prepare statement and run the query directly (commented out) it updates, when I try and run it as a prepare statement it returns an error "MySQL server has gone away".
非常感谢任何帮助我已经没有想法了!
Any help very much appreciated I've run out of ideas!
PS:本地主机上的 Apache/XAMPP
PS: Apache / XAMPP on localhost
推荐答案
我查看了你的代码,发现了这个.
I have looked at your code and I found this.
$stmt = $this->conn()->prepare("UPDATE tblorderhdr SET completed = ? WHERE orderid = ?");
我写的几乎一样.但是我看到你把连接和准备功能分开了.
I wrote nearly the same. But I saw you separated the connection from the prepare function.
$db = $this->conn();
$stmt = $db->prepare("UPDATE tblorderhdr SET completed = ? WHERE orderid = ?");
我也不知道为什么,但现在可以了.
I don't know either why, but it works now.
这篇关于mysqli 准备语句错误“MySQL 服务器已消失"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!