执行预准备语句时,我收到以下错误
(2013)在queryexception期间丢失了与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'中执行prepared语句时,它会在没有任何错误的情况下执行,但是当我在方法“execute_insert”中执行它时它会失败,并显示错误: -
(2013)在查询期间丢失与MySQL服务器的连接
执行前准备好的语句的var_dump
对象(mysqli_stmt)#4(10){[“affected_rows”] =&gt; int(0)[“insert_id”] =&gt; int(0)[“num_rows”] =&gt; int(0)[“param_count”] =&gt; int(5)[“field_count”] =&gt; int(0) [“errno”] =&gt; int(2013)[“error”] =&gt; string(44)“查询期间与MySQL服务器的连接丢失”[“error_list”] =&gt; array(1){[0] =&gt; array(3){[“errno”] =&gt; int(2013)[“sqlstate”] =&gt; string(5)“HY000”[“error”] =&gt; string(44)“查询期间与MySQL服务器的连接丢失”}} [“sqlstate”] =&gt; string(5)“HY000”[“id”] =&gt; int(1)}
有人可以帮忙吗?
答案 0 :(得分: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 style(问题中使用了程序风格)
注意没有新
$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) {
....
}