1

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

KePimbo
  • 53
  • 7
  • take a look here: http://stackoverflow.com/questions/7942154/mysql-error-2006-mysql-server-has-gone-away it can be timeout or small packetsize, are you getting it every time? – Milan Halada May 16 '14 at 13:24
  • See this answer also http://stackoverflow.com/a/12792977/ also check the MySQL.com page http://dev.mysql.com/doc/refman/5.0/en/gone-away.html --- Googling the error message produced many results. – Funk Forty Niner May 16 '14 at 13:28
  • As I said I have tried it using ini_set,now I have also tried to modify the max_allowed_packet to 16M and the same error.I have googled a lot of before asking,and what I found was you posted to me – KePimbo May 16 '14 at 13:31
  • 2
    wait, is `$db = $db->prepare` viable in mysqli? Couldnt reassigning `$db` variable cause disconnection? could you try to assign it to another variable? `$stmt` or something... – Milan Halada May 16 '14 at 13:33
  • 1
    @KePimbo Uriel_SVK has a point there. You seem to be overwriting your `$db` variable. See this answer http://stackoverflow.com/a/19599838/ – Funk Forty Niner May 16 '14 at 13:36
  • @Uriel_SVK Yes,I was seeing the code when found that.Thanks it solved the problem. – KePimbo May 16 '14 at 13:40

2 Answers2

3

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();
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Milan Halada
  • 1,943
  • 18
  • 28
1

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');
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Daan
  • 12,099
  • 6
  • 34
  • 51
  • Yes I have got a database called admin and the root with no password (But because it is my computer,in which network I am only and mysql port closed in the router it doesn't matter) – KePimbo May 16 '14 at 13:20