php mysql server gone away - php

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

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();

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');

Related

linking mit inventor2 to sql database error 1109

I have been trying to connect my hosted sql database with mitinventor2 in a registeration form and I am getting an error that says something about
URL. error 1109
This is my php code :
<?php
DEFINE ('DBUSER', '******');
DEFINE ('DBPW', '*****');
DEFINE ('DBHOST', 's******');
DEFINE ('DBNAME', '*******');
$dbc = mysqli_connect(DBHOST,DBUSER,DBPW);
if (!$dbc) {
die("Database connection failed: " . mysqli_error($dbc));
exit();
}
$dbs = mysqli_select_db($dbc, DBNAME);
if (!$dbs) {
die("Database selection failed: " . mysqli_error($dbc));
exit();
}
$clientusername = mysqli_real_escape_string($dbc, $_GET['clientusername']);
$clientemail = mysqli_real_escape_string($dbc,$_GET['clientemail']);
$clientpno = mysqli_real_escape_string($dbc,$_GET['clientpno']);
$clientpass = mysqli_real_escape_string($dbc,$_GET['clientpass']);
$query = "INSERT INTO ssudb(clientusername, clientemail, clientpno, clientpass) VALUES ('$clientusername', '$clientemail', '$clientpno', '$clientpass')";
$result = mysqli_query($dbc, $query) or trigger_error("Query MySQL Error: " . mysqli_error($dbc));
mysqli_close($dbc);
?>
I am sure about the database info yet I hide them for security reasons. My database is well structured with phpmyadmin.
The link to the php file is right with no errors, and here is my mit inventor block:
I hope I didn't validate any rule.
Thanks in advance!
note:
when open phpmyadmin somtimes i see the table is having new attributes with no values in it but the increment ID.
$clientlocation is missing in the VALUES

php manipulations with mysql

I did connection to the DB with PHP code, everything ok. BUT i can't do any manipulations in DB with PHP code, f.e. add/edit tables.
here is piece of code:
<?php
$mysqli = new mysqli("localhost", "root", "mypass", "mybase");
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error);
}
$mysqli->query("SET NAMES 'utf8'");
// editing existing table
$success = $mysqli->query ("INSERT INTO `myBase`.`users` (`login`, `password`, `reg_date`) VALUES ('aaa', '".md5("123")."', '".time()."')");
echo $success; // this echo does not work
$mysqli->close();
?>
No new user after this edit.
Try debugging with
echo $mysqli->error;
Generally, errors that prevent you from inserting into your table are syntax or user database permissions.

Cannot connect to local database using php

I am having trouble connecting to my localhost database with php. It feels like I have followed every tutorial there is.
current php code:
<?php
//ENTER YOUR DATABASE CONNECTION INFO BELOW:
$hostname="localhost";
$database="webutvshop";
$username="dbconnect";
$password="password";
//DO NOT EDIT BELOW THIS LINE
$link = mysql_connect($hostname, $username, $password);
if (!$link) {
die('Connection failed: ' . mysql_error());
}
else{
echo "Connection to MySQL server " .$hostname . " successful!
" . PHP_EOL;
}
$db_selected = mysql_select_db($database, $link);
if (!$db_selected) {
die ('Can\'t select database: ' . mysql_error());
}
else {
echo 'Database ' . $database . ' successfully selected!';
}
mysql_close($link);
?>
The issue stands, when I acess the file locally on my computer I do not get any answer at all from it. I've tried with many other, yet I do not get any answers from them!
I need help in order to keep working on my schoolproject, thanks.
Stop using mysql_*, because they are deprecated officially. Use mysqli_* or PDO for this purpose. An example:-
<?php
//ENTER YOUR DATABASE CONNECTION INFO BELOW:
$hostname="localhost";
$database="stackquestion";
$username="root";
$password=""; // check once with empty password and once with some password that you tried already
//DO NOT EDIT BELOW THIS LINE
$link = mysqli_connect($hostname, $username, $password);
if (!$link) {
die('Connection failed: ' . mysqli_connect_error());
}
else{
echo "Connection to MySQL server " .$hostname . " successful!
" . PHP_EOL;
}
$db_selected = mysqli_select_db($link,$database);
if (!$db_selected) {
die ('Can\'t select database: ' . mysqli_error($link));
}
else {
echo 'Database ' . $database . ' successfully selected!';
}
mysqli_close($link);
?>
Output:- http://prntscr.com/7cbr5j
Can you also verify you can connect to the database from a command line:
mysql -u dbconnect -ppassword -h localhost webutvshop

mysql_pconnect not working

I have created a php connection file as explorecalirfornia.php content as below.
#FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
$hostname_explorecalirfornia = "localhost";
$database_explorecalirfornia = "explorecalirfornia";
$username_explorecalirfornia = "root";
$password_explorecalirfornia = "";
$explorecalirfornia = mysql_pconnect($hostname_explorecalirfornia, $username_explorecalirfornia, $password_explorecalirfornia) or trigger_error(mysql_error(),E_USER_ERROR);
I have include this php file in my page using command
<?php require_once('Connections/explorecalifornia.php'); ?>
when I test the php code using local host following error is throwing.
Please help me to complete my tutorial.
Either #mysql_pconnect($hostname_explorecalirfornia, $username_explorecalirfornia, $password_explorecalirfornia) or trigger_error(mysql_error(),E_USER_ERROR); //# infront of mysql_pconnect or use below written code
$mysqli = new mysqli(HOST, USERNAME, PASSWORD, DBNAME);
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
mysql_ *functions are deprecated and will no longer be used in the future. Use mysqli_* functions or PDO instead. Befriend The PHP Manual.
$explorecalirfornia = new mysqli($hostname_explorecalirfornia, $username_explorecalirfornia, $password_explorecalirfornia);
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}

Cant see data after importing in _post form

Hey i am using Mamp on imac and my problem is that when i hit the submit button (on a post form) to enter the data then nothing shows up and the database remains empty.
Here is my code :
<?php
define('DB_NAME', 'demob');
define('DB_USER','brom');
define('DB_PASSWORD','****');
Define('DB_HOST','localhost');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link){
die('Could not connect : ' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected){
die('cant use' . DB.NAME . ' : ' .mysql_error());
}
$value = $_POST['input1'];
$sql = "INSER INTO memberss ('input1') VALUES ('$value')";
mysql_close();
?>
You are not executing a query.
$sql = "INSER INTO memberss ('input1') VALUES ('$value')";
mysql_query($sql);
You should know that, the method you are using to connect to mysql is deprecated now. please read up about PDO or mysqli

Categories