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) ["errno"]=> int(2013) ["error"]=> string(44) "Lost connection to MySQL server during query" ["error_list"]=> array(1) { [0]=> array(3) { ["errno"]=> int(2013) ["sqlstate"]=> string(5) "HY000" ["error"]=> string(44) "Lost connection to MySQL server during query" } } ["sqlstate"]=> string(5) "HY000" ["id"]=> int(1) }
Could someone please help?
prepare_sanitized_insert()
beforeexecute_insert()
? – 01emysqli_connect
to assign db_sqli inprepare_sanitized_insert()
, anddb_factory::get_db_sqli()
inexecute_insert()
? Can you log/echo in that if statement to see if it's trying to reassign the variable? – aynberget_db_sqli()
in the execute call? Seems like you're using multiple DIFFERENT methods of connecting to the db. I doubt that a statement prepared on one mysql connection is usable via another separate connection. – Marc B