首页 > 解决方案 > 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 的了解并不能让我找出解决方案。

谢谢!

标签: php

解决方案


为了证明我的意见

如果某些列名(或全部)在 SQL 中具有特殊含义,则在它们周围使用反引号是明智的 - 例如nametime

$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
    }

推荐阅读