-2

I'm getting these errors:

Warning: mysqli_query(): MySQL server has gone away in (local db)

Warning: mysqli_query(): Error reading result set's header in (local db)

I am establishing a connection at first:

$connection = new mysqli($server, $user, $pass, $db) or die("unable");

Then this:

$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 looks like this:

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

Error occurs at the second sql->execute. My guess is that it's because I'm using the connection for several requests but my knowledge of PHP does not allow me to figure out a solution.

Thank you!

J. Doe
  • 571
  • 1
  • 10
  • 24
  • Thanks progman for marking it as a duplicate but it didn't solve my problem to increase max allowed packets. Perhaps you can help in other ways other than being a school hall monitor? – J. Doe Jan 29 '19 at 22:20
  • 1
    try freeing the statement object - ie `$sql->close();` - also, assign the result of `$sql->execute()` to a variable and test that rather than $sql – Professor Abronsius Jan 29 '19 at 22:20
  • Thanks @RamRaider, it solved my problem. Unfortunately it opened up a new one.. – J. Doe Jan 29 '19 at 22:26

1 Answers1

1

To demonstrate my comments

It would be wise to use backticks around certain column names ( or all ) if they have special meaning within SQL - such as name or 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
    }
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46