简体   繁体   中英

Lost connection to MySQL server during query exception

I am getting the below error while executing the prepared statement

(2013) Lost connection to MySQL server during queryexception
I have checked almost all the questions posted on this topic before posting but could not find the answer. so please do not close.

I am new to PHP and MYSQL so please correct if I have made any mistake
MY Code:-

<?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;
}


}

When I execute the prepared statement inside the method 'prepare_sanitized_insert', it gets executed without any error but when I execute it inside the method "execute_insert" it fails with the error :-

(2013) Lost connection to MySQL server during query

var_dump of the prepared statement just before the execution
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) } }

Could someone please help?

you mixed up object-oriented style and procedural style .

object-oriented style
Note the new

$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(), ...)";

wrong

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

correct

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

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

Prozedural style (The procedural style was used in the question)
Note without new

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

wrong

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

correct

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

wrong

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

correct

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

wrong

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

correct

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

You have to decide for one of the two object-oriented style or procedural style .


you could use a constructor (also mentioned @Saber Haj Rabiee)
object-oriented style

 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;
    }
  }

call it like

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM