- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
好的。这是一个奇怪的。首先,这是错误:
Fatal error: Uncaught mysqli_sql_exception: MySQL server has gone away in database.php:84 Stack trace: #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 Server 还是 PHP 的问题?
编辑:下面是我如何使用数据库类(getConnection()
只是返回一个新的数据库实例)
function getUsers() {
$query = getConnection()->query('SELECT * FROM `users`');
$query->execute();
return $query->getRows();
}
最佳答案
我想我现在知道是什么导致了这个问题!我希望我是对的。
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();
但作为一个正确的解决方案,您必须在整个脚本执行过程中保持单个连接处于事件状态,并将其用于所有查询。
关于php - 执行 mysqli_stmt->execute() 导致 "MySQL server has gone away"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49355763/
我正在尝试使用 Spark 从 Cassandra 读取数据。 DataFrame rdf = sqlContext.read().option("keyspace", "readypulse
这是代码: void i_log_ (int error, const char * file, int line, const char * fmt, ...) { /* Get erro
我必须调试一个严重依赖 Gtk 的程序。问题是由于某些原因,在使用 GtkWindow 对象时开始出现许多运行时警告。问题是,即使 Gtk 提示严重错误,它也不会因这些错误而中止。我没有代码库的更改历
我正在尝试从已有效编译和链接的程序中检索二进制文件。我已经通过 GL_PROGRAM_BINARY_LENGTH 收到了它的长度。该文档说有两个实例可能会发生 GL_INVALID_OPERATION
我有一个托管在 Azure 环境中的服务。我正在使用控制台应用程序使用该服务。这样做时,我得到了异常: "The requested service, 'http://xxxx-d.yyyy.be/S
我有以下代码,它被 SEGV 信号杀死。使用调试器表明它被 main() 中的第一个 sem_init() 杀死。如果我注释掉第一个 sem_init() ,第二个会导致同样的问题。我试图弄清楚是什么
目前我正在编写一个应用程序(目标 iOS 6,启用 ARC),它使用 JSON 进行数据传输,使用核心数据进行持久存储。 JSON 数据由 PHP 脚本通过 json_encode 从 MySQL 数
我对 Xamarin.Forms 还是很陌生。我在出现的主页上有一个非常简单的功能 async public Task BaseAppearing() { if (UserID
这是我的代码的简化版本。 public class MainActivity extends ActionBarActivity { private ArrayList entry = new Arr
我想弄明白为什么我的两个 Java 库很难很好地协同工作。这是场景: 库 1 有一个类 A,其构造函数如下: public A(Object obj) { /* boilerplate */ } 在以
如果网站不需要身份验证,我的代码可以正常工作,如果需要,则在打印“已创建凭据”后会立即出现 EXC_BAD_ACCESS 错误。我不会发布任何内容,并且此代码是直接从文档中复制的 - 知道出了什么问题
我在使用 NSArray 填充 UITableView 时遇到问题。我确信我正在做一些愚蠢的事情,但我无法弄清楚。当我尝试进行简单的计数时,我得到了 EXC_BAD_ACCESS,我知道这是因为我试图
我在 UITableViewCell 上有一个 UITextField,在另一个单元格上有一个按钮。 我单击 UITextField(出现键盘)。 UITextField 调用了以下方法: - (BO
我有一个应用程序出现间歇性崩溃。崩溃日志显示了一个堆栈跟踪,这对我来说很难破译,因此希望其他人看到了这一点并能为我指出正确的方向。 基本上,应用程序在启动时执行反向地理编码请求,以在标签中显示用户的位
我开发了一个 CGImage,当程序使用以下命令将其显示在屏幕上时它工作正常: [output_view.layer performSelectorOnMainThread:@selector(set
我正在使用新的 EncryptedSharedPreferences以谷歌推荐的方式上课: private fun securePrefs(context: Context): SharedPrefe
我有一个中继器,里面有一些控件,其中一个是文本框。我正在尝试使用 jquery 获取文本框,我的代码如下所示: $("#").click(function (event) {}); 但我总是得到 nu
在以下场景中观察到 TTS 初始化错误,太随机了。 已安装 TTS 引擎,存在语音集,并且可以从辅助功能选项中播放示例 tts。 TTS 初始化在之前初始化和播放的同一设备上随机失败。 在不同的设备(
maven pom.xml org.openjdk.jol jol-core 0.10 Java 类: public class MyObjectData { pr
在不担心冲突的情况下,可以使用 MD5 作为哈希值,字符串长度最多为多少? 这可能是通过为特定字符集中的每个可能的字符串生成 MD5 哈希来计算的,长度不断增加,直到哈希第二次出现(冲突)。没有冲突的
我是一名优秀的程序员,十分优秀!