好。这很奇怪。首先,这是错误:
致命错误:未捕获mysqli_sql_exception:MySQL服务器已经在database.php中消失了:84 堆栈跟踪: #0 database.php(84):mysqli_stmt-> execute()
根据其他StackOverflow文章,例如this和this,该错误MySQL server has gone away
意味着:
但是,我已将超时和最大数据包大小变量设置为其最大值,而查询只是从空表中进行选择。没有理由认为其中任何一个应该是一个问题。我也从Python验证 - 服务器可以连接到,查询应该能够执行。它甚至可以在phpMyAdmin中正常工作。
在mysqli_stmt::prepare
的PHP文档中,它说明了错误:
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服务器还是PHP的问题?
编辑:以下是我使用Database类的方法(getConnection()
只返回一个新的数据库实例)
function getUsers() {
$query = getConnection()->query('SELECT * FROM `users`');
$query->execute();
return $query->getRows();
}
答案 0 :(得分:2)
我想我现在知道造成这个问题的原因了!我希望我是对的。
function getUsers() {
// here you are creating a Database instance,
// but you've left it in the air, because you just retrieved `query` from it.
$query = getConnection()->query('SELECT * FROM `users`');
// at this point, there are already NO Database instance,
// because you have no reference of it.
// thus what I've read about resources being garbage collected
// will now apply here.
$query->execute();
// this will fail, indeed, Mysql has gone away!
return $query->getRows();
}
您应该至少将连接保存到另一个变量。
$conn = getConnection();
$query = $conn->query('SELECT * FROM `user`');
$query->execute();
return $query->getRows();
但作为一个正确的解决方案,您必须通过整个脚本执行保持单个连接存活,并将其用于所有查询。