在查询异常期间丢失与 MySQL 服务器的连接


Lost connection to MySQL server during query exception

我在执行预准备语句时收到以下错误

(2013) 查询异常期间与 MySQL 服务器的连接丢失
在发布之前,我已经检查了几乎所有关于此主题的问题,但找不到答案。 所以请不要关闭。

我是PHP和MYSQL的新手,所以如果我犯了任何错误,请纠正
我的代码:-

<?php
class sanitize_insert{
protected $prepared_stmt;
protected $db_sqli;
public function prepare_sanitized_insert($created_by)
{
    if(!is_integer($created_by))
    {
        throw new InvalidArgException("Invalid argument(s) type. Expected integer(s)"); //to be defined
    }
    
    
    
    $query = "insert into requests_v(user_id,property_id,request_type,description,to_user_id,created_on,last_update_date,created_by,last_updated_by) values (?,?,?,?,?,now(),now(),8,8);";
            if(!is_resource($this->db_sqli))
    {
        $this->db_sqli = mysqli_connect('host','user','password','dbname');
    }
    if(!$this->prepared_stmt = $this->dbh->prepare($query))
    {
        return false;   
    }
    $this->prepared_stmt->bind_param('iiisi', $user_id, $property_id, $req_type, $desc,$to_id);
    //$result = $this->db_sqli->execute($this->prepared_stmt);
    return true;
}
public function execute_insert()
{
    if(!is_object($this->prepared_stmt))
    {
        return false;
    }
    if(!is_resource($this->db_sqli))
    {
        $this->db_sqli = mysqli_connect('host','user','password','dbname');
    }
    $result = $this->prepared_stmt->execute();
    return $result;
}

}

当我在方法"prepare_sanitized_insert"中执行准备好的语句时,它会执行而没有任何错误,但是当我在方法"execute_insert"中执行它时,它会失败并显示错误:-

(2013) 查询期间与 MySQL 服务器的连接丢失

在执行前var_dump准备好的语句object(mysqli_stmt)#4 (10) { ["affected_rows"]=> int(0) ["insert_id"]=> int(0) ["num_rows"]=> int(0) ["param_count"]=> int(5) ["field_count"]=> int(0) ["errno"]=> int(2013) ["error"]=> string(44) "查询期间失去与 MySQL 服务器的连接" ["error_list"]=> array(1) { [0]=
> array(3) { ["errno"]=> int(2013

) ["sqlstate"]=> string(5) "HY000" ["error"]=> string(44) "查询期间丢失与 MySQL 服务器的连接" } } } ["sqlstate"]=> string(5) "HY000" ["id"]=> int(1) }

有人可以帮忙吗?

你混淆了面向对象的风格和过程风格。

面向对象的样式
注意新的

$this->db_sqli = new mysqli('host','user','password','dbname');
if (mysqli_connect_errno()) {
   printf("Connect failed: %s'n", mysqli_connect_error());
   exit();
}
$query = "insert into requests_v(user_id, ...) values (?,?,?,?,?,now(), ...)";

if(!$this->prepared_stmt = $this->dbh->prepare($query)) {

正确

if ($this->prepared_stmt = $this->db_sqli->prepare($query)) {
    $this->prepared_stmt->bind_param('iiisi', $user_id, $property_id, ..., ...);

Prozedural风格(问题中使用了程序风格)
没有新的注释

$this->db_sqli = mysqli_connect('host','user','password','dbname');
...
$query = "insert into requests_v(user_id, ...) values (?,?,?,?,?,now(), ...)";

if (!$this->prepared_stmt = $this->db_sqli->prepare($query)) {

正确

if (!$this->prepared_stmt = mysqli_prepare($this->db_sqli, $query)) {

$this->prepared_stmt->bind_param('iiisi', $user_id, $property_id, ...,...);

正确

mysqli_stmt_bind_param($this->prepared_stmt,'iiisi',$user_id, ..., ...,...);

$result = $this->prepared_stmt->execute();

正确

$result = mysqli_stmt_execute($this->prepared_stmt);

您必须决定两种object-oriented style之一或procedural style


你可以使用构造函数(也提到@Saber Haj Rabiee)
面向对象的样式

 class sanitize_insert{
   protected $prepared_stmt;
   protected $db_sqli;
   public    $OK = TRUE;
  public function __construct($host, $user, $pass, $db)
  { 
    $this->db_sqli = new mysqli($host, $user, $pass, $db); 
    if (mysqli_connect_errno()) {
      printf("Connect failed: %s'n", mysqli_connect_error());
      $this->OK = FALSE;
    }
  }

称呼它为喜欢

 $sanitizeclass = new sanitize_insert($host, $user, $pass, $db);
 if ($sanitizeclass->OK) {
  ....
 }