php mysql server gone away

2019-06-04 19:55发布

问题:

I have got a problem with a script that I am developing.I'm testing it on windows XAMPP and I have got a database and a table which I am trying to insert a row.I am using the mysqli extension and using prepared statements to prevent sql injection.But when I execute the query I gets Error 2006:MySQL server gone away,but the server is working well.The server is my own computer.So I don't know what happens.

This is the code I am using:

<?php
header('Content-type: text/html; charset=utf-8');
if(isset($_GET['user']) && isset($_GET['pass'])){
    $db = new mysqli("localhost", "root", "", "admin");
    if ($db->connect_errno) {
        echo "Falló la conexión a MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
    }
    if (!($db = $db->prepare("INSERT INTO logins(user,pass,ip,url) VALUES (?,?,?,?)"))) {
        echo "Falló la preparación: (" . $mysqli->errno . ") " . $mysqli->error;
    }
    if (!$db->bind_param('ssss', $_GET['user'],  $_GET['pass'],$_SERVER['REMOTE_ADDR'],$_SERVER["HTTP_REFERER"])){
        echo "Falló la vinculación de parámetros: (" . $db->errno . ") " . $db->error;
    }
    if (!$db->execute()) {
        echo "Falló la ejecución: (" . $db->errno . ") " . $db->error;
    }
    $db->close();
}

?>

Thanks

EDIT:I forgot to put that I have tried to use the ini_set trick,but didn't worked

回答1:

I think the problem is $db = $db->prepare, reassigning $db variable probably causes disconnect. Try to write it this way:

$db = new mysqli("localhost", "root", "", "admin");
$stmt = $db->prepare("INSERT INTO logins(user,pass,ip,url) VALUES (?,?,?,?)");
$stmt->bind_param('ssss', $_GET['user'],  $_GET['pass'],$_SERVER['REMOTE_ADDR'],$_SERVER["HTTP_REFERER"]);
$stmt->execute();
$db->close();


回答2:

Your connection string is wrong it should be like this: (unless you have a database called admin and no password)

$db = new mysqli('localhost', 'my_user', 'my_password', 'my_db');


标签: php mysql mysqli