【问题标题】:Lost connection to MySQL server during query exception在查询异常期间丢失与 MySQL 服务器的连接
【发布时间】:2013-09-22 06:46:35
【问题描述】:

执行准备好的语句时出现以下错误

(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"]=> 数组(1) { [0]=> 数组(3) { ["errno"]=> int(2013) ["sqlstate"]=> 字符串(5) "HY000" [ "error"]=> string(44) "Lost connection to MySQL server during query" } } ["sqlstate"]=> string(5) "HY000" ["id"]=> int(1) }

有人可以帮忙吗?

【问题讨论】:

  • 你在execute_insert()之前执行过prepare_sanitized_insert()吗?
  • 是的,我在 execute_insert() 之前执行了 prepare_sanitized_insert()。
  • 你为什么用mysqli_connectprepare_sanitized_insert()中分配db_sqli,在execute_insert()中分配db_factory::get_db_sqli()?您可以在该 if 语句中记录/回显以查看它是否正在尝试重新分配变量吗?
  • 为什么你在prepare函数中有一个mysqli_connect()调用,而在execute调用中却使用get_db_sqli()?似乎您正在使用多种不同的方法连接到数据库。我怀疑在一个 mysql 连接上准备的语句是否可以通过另一个单独的连接使用。
  • 抱歉给您带来了困惑!!!早些时候我使用工厂类来获取 my_sqli 对象。我已经编辑了代码,请看一下。我也在更新准备好的语句的 var_dump。

标签: php mysql sql


【解决方案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 styleprocedural 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) {
  ....
 }

【讨论】:

  • 感谢您的帮助。最初我只使用 oo 方法,但后来我遇到了这个错误,我将数据库连接代码转移到这个文件并混合了两种样式,同时尝试消除错误的可能性。
猜你喜欢
  • 2012-11-15
  • 2015-09-22
  • 1970-01-01
  • 2015-11-22
  • 1970-01-01
  • 2020-07-11
  • 2015-06-27
  • 1970-01-01
  • 2020-12-05
相关资源
最近更新 更多