php - mysqli_query(): MySQL 服务器已经消失
问题描述
我收到这些错误:
Warning: mysqli_query(): MySQL server has gone away in (local db)
Warning: mysqli_query(): Error reading result set's header in (local db)
我首先建立一个连接:
$connection = new mysqli($server, $user, $pass, $db) or die("unable");
然后这个:
$sql = $connection->prepare("INSERT INTO comments (name,mail,homepage,comment,time) VALUES (?,?,?,?,?)");
$sql->bind_Param('sssss',$name,$mail,$homepage,$comment,$time);
$sql->execute();
if($sql){
if(!addPics($connection, $image_content, $mime, $time)){
//other code
}
addPics 看起来像这样:
function addPics($connection, $image_content, $mime, $time){
$sql = $connection->prepare("INSERT INTO pictures (picture, mime, time) VALUES (?,?,?)");
$sql->bind_Param('sss',$image_content,$mime, $time);
$sql->execute();
if($sql){
return true;
} else {
return false;
}
第二个 sql->execute 发生错误。我的猜测是,这是因为我将连接用于多个请求,但我对 PHP 的了解并不能让我找出解决方案。
谢谢!
解决方案
为了证明我的意见
如果某些列名(或全部)在 SQL 中具有特殊含义,则在它们周围使用反引号是明智的 - 例如name
或time
$sql = $connection->prepare("INSERT INTO comments (`name`,`mail`,`homepage`,`comment`,`time`) VALUES (?,?,?,?,?)");
$sql->bind_Param('sssss',$name,$mail,$homepage,$comment,$time);
/* assign a variable to the `execute` method and test that var */
$result = $sql->execute();
if( $result ){
/* the statement is now finished with, close it */
$sql->close();
if(!addPics($connection, $image_content, $mime, $time)){
//other code
}
推荐阅读
- python - 如何在pandas python中查找和匹配来自不同数据帧的特定值
- python - 对字典进行排序,然后通过排名替换值
- node.js - nodejs中如何根据条件编写多个返回条件
- mysql - Mysql - 获取列值是除特定值之外的所有内容的记录
- python - 如果字符串值由字符和数字组成,是否有返回 True 的字符串方法?
- bash - 适用于 Windows 的 Linux 子系统 - shell 脚本 scp 将所有文件重命名为“.”
- home-assistant - 根据 Homeassistant 控制的湿度读数打开风扇
- python - 无法更新 us-central1 中的加密密钥
- amazon-web-services - IAM 角色 - AssumeRolepolicy 包含无效的委托人:“STAR”:“*”
- javascript - 在我的 db.collection forEach 上使用 Promise.all