【发布时间】:2018-08-27 14:06:39
【问题描述】:
好的。这是一个奇怪的问题。首先,这是错误:
致命错误:未捕获的 mysqli_sql_exception:MySQL 服务器已在 database.php:84 中消失 堆栈跟踪: #0 database.php(84): mysqli_stmt->execute()
根据其他 StackOverflow 文章,例如 this 和 this,那个错误,MySQL server has gone away 意味着:
- mysqli 对象在没有被关闭的情况下被重用(不,绝对不是)
- MySQL 服务器端的连接超时变量太小
- 最大数据包大小变量太小
但是,我已将超时和最大数据包大小变量设置为它们的最大值,并且查询只是从一个空表中进行选择。没有理由为什么这两个应该是一个问题。我还通过 Python 进行了验证——可以连接到服务器并且应该能够执行查询。它甚至可以在 phpMyAdmin 中正常工作。
在mysqli_stmt::prepare 的 PHP 文档中,关于错误的说明如下:
- 我在Linux上使用mysqlnd,当语句长于
max_allowed_packet时不应该给出这个错误 - 我已经提到我如何将
max_allowed_packet设置为变量的最大值。
如果您希望我提供更多信息,例如我的 SQL 服务器或 PHP 配置,请告诉我您需要什么。
我读过的一篇文章说使用mysqli->ping() 来检查连接的情况,在我调用mysqli_stmt->execute() 之前它似乎没问题。
我很确定这是我的实现问题——我尝试重新安装 Web 服务器和 MySQL 服务器,切换 PHP 版本,甚至尝试切换主机。但尽管我尝试解决此问题,但我仍然收到错误。
代码如下:
<?php
ini_set('mysql.connect_timeout', 3000);
ini_set('default_socket_timeout', 3000);
/* define the database class */
class Database {
public $host = 'localhost';
public $name = '';
public $user = '';
public $pass = '';
private $mysqli;
/* constructor function, inits the database connection */
function __construct($chost, $cname, $cuser, $cpass) {
$this->host = $chost;
$this->name = $cname;
$this->user = $cuser;
$this->pass = $cpass;
mysqli_report(MYSQLI_REPORT_ALL);
$this->mysqli = new mysqli($this->getHost(), $this->getUsername(), $this->getPassword(), $this->getName());
}
/* closes the connection to the database */
function close() {
return $this->getMySQLi()->close();
}
/* returns a query object for the given parameters */
function query($query, $type='', ...$params) {
$statement = $this->getMySQLi()->prepare($query);
if(strlen($type) != 0) {
// bind parameters to query
$statement->bind_param($type, ...$params);
}
/*
* stackoverflow readers: this the debug code
* I mentioned to check the connection
*
*/
if ($this->getMySQLi()->ping()) {
printf ("Our connection is ok!\n");
} else {
printf ("Error: %s\n", $this->getMySQLi()->error);
}
return new Query($statement);
}
/* getter functions */
function getMySQLi() {
return $this->mysqli;
}
function getHost() {
return $this->host;
}
function getName() {
return $this->name;
}
function getUsername() {
return $this->user;
}
function getPassword() {
return $this->pass;
}
}
/* define the query class */
class Query {
private $statement;
private $result;
/* constructor, sets variables and stuff */
function __construct($statement) {
$this->statement = $statement;
}
/* executes the statement */
function execute() {
$status = $this->getStatement()->execute();
$this->result = $this->getStatement()->get_result();
return $status;
}
/* closes the statement */
function close() {
return $this->getStatement()->close();
}
/* returns the number of results */
function countRows() {
return $this->getResult()->num_rows;
}
/* getter functions */
/* returns the statement object */
function getStatement() {
return $this->statement;
}
/* returns the result object */
function getResult() {
return $this->result;
}
function getRow() {
return $this->getResult()->fetch_assoc();
}
/* returns the result in an array */
function getRows() {
$rows = array();
while($row = $this->getRow()) {
$rows[] = $row;
}
return $rows;
}
}
?>
所以。我的问题是,我的实施有问题吗?如何缓解问题?是 SQL server 的问题还是 PHP 的问题?
编辑:这是我使用 Database 类的方法(getConnection() 只是返回一个新的 Database 实例)
function getUsers() {
$query = getConnection()->query('SELECT * FROM `users`');
$query->execute();
return $query->getRows();
}
【问题讨论】:
-
您在哪里使用了导致此错误的这些类?我复制并粘贴了您的代码并尝试了它,它对我来说很好。
-
@Wreigh 我编辑了问题并在底部添加了使用此类的代码。如果这也适用于您,请问您使用的是什么 PHP 版本、网络服务器和模块?谢谢!
-
php 7.1.7,我只是使用 php 构建的服务器。这就是我使用它的方式。
$a = new Database('localhost', 'test_suites', 'root', ''); $b = $a->query("SELECT * FROM test_suites"); $b->execute(); -
哦,对不起。我已经评论了
mysqli_report()这为什么它有效!哈哈等等。 -
@Wreigh 没办法。我想你刚刚找到了这个错误的解决方案,我花了很多时间试图修复它。我不敢相信,但它现在正在工作。我所做的只是通过查询的构造函数传递数据库对象并将其保存在一个变量中(然后在执行之前检查是否为空),然后繁荣,它工作。万分感谢!如果您发布答案,我会将其标记为正确(一旦允许)