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