Mysql server Gone Away - php

I got "MySQL server gone away" error, i also got the resource id & connection also created successfully. Really don't know what's going wrong.
below is my code..
$query ='select * from table';
if (!#mysql_ping($conn)) {
conn = mysql_connect(DB_SERVER, DB_SERVER_USERNAME, DB_SERVER_PASSWORD) or die("Unable to connect with server");
mysql_select_db(DB_DATABASE,$conn) or die("Unable to select database");
mysql_query($query) or mysql_error($conn);
}
Any help? i miss something in or i need to set up some config file?

if (!#mysql_ping($conn)) {
mysql_close($conn);
$conn = mysql_connect(DB_SERVER, DB_SERVER_USERNAME, DB_SERVER_PASSWORD) or die("Unable to connect with server");
echo 'connection created--' . $conn . '---<br/>';
mysql_select_db(DB_DATABASE, $conn) or die("Unable to select database" . mysql_error($conn));
}

Related

Error when trying to access database on server: Lost connection to MySQL server

I am trying to access a database on my web host with a php file that is on the same server. When trying to load the page I get the following error: mysql_connect(): Lost connection to MySQL server at 'reading initial communication packet', system error: 111. I cannot find a good answer what it going wrong. Below is my php that I am using. I have my IP address of my database set as my host name.
<?php
$con = mysql_connect("10.123.0.209:3306","username", "password");
if (!$con){
die("cannot connect: " . mysql_error());
}
mysql_select_db("matmac78_macy", $con);
$sql = "SELECT * FROM countries";
$mydata = mysql_query($sql, $con);
while ($record = mysql_fetch_array($mydata)){
echo $record['country'] . " " . $record['population']
echo"<br/>";
}
mysql_close($con);
?>
Any help would be greatly appreciated!
You should switch to using PHP's PDO connection protocol.
$username = 'username';
$password = 'password';
$dbName = 'matmac78_macy';
$host = '10.123.0.209:3306';
try {
$this->database = new \PDO( "mysql:host={$this->host};dbname={$this->dbName}", $this->username, $this->password );
$this->database->setAttribute( \PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION );
$this->database->setAttribute( \PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_ASSOC );
return $this->database;
} catch (\PDOException $e) {
throw new \Exception( "Could not establish database connection. \n" ); #Push error message
}

mysql_select_db failure

I am using phpMyAdmin + MySQL.
I created a database and am now trying to make the connection in a PHP script. The curious thing is that connecting to the DB works, so I get the "Connected to MySQL server" message, but I when it comes to selecting the 'petfood' database, the script shows "DIED at selection".
Any idea why? Thanks, and here's my piece of code:
<?php
$user = 'localhost';
$pass = 'password';
$db_name = 'petfood';
$db_conn = new mysqli("localhost", $user, $pass, $db_name) or die("Cannot connect to DB");
echo "Connected to MySQL server";
mysql_select_db($db_name) or die("DIED at selection");
echo "Database Selected";
?>
Spot the difference:
$db_conn = new mysqli("localhost", $user, $pass, $db_name) or die("Cannot connect to DB");
^----
mysql_select_db($db_name) or die("DIED at selection");
^---
If you had proper debugging, you'd have been told about the problem:
mysql_select_db($db_name) or die(mysql_error());
^^^^^^^^^^^^^^
Never output a fixed (useless) error message when you can have the system TELL you what's wrong.
1: using mysql
$dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");
mysql_select_db("examples",$dbhandle) or die("Could not select examples");
$query = "SELECT name FROM mytable" ;
$result = mysqli_query($query);
2: using mysqli
$link = mysqli_connect("myhost","myuser","mypassw","mybd") or die("Error " . mysqli_error($link));
$query = "SELECT name FROM mytable" ;
$result = mysqli_query($link, $query);

Strange behaviour when connect to database in php/mysql

I'm connecting to Mysql Database With following code. I'm using mysqli. But It's not connecting to mysql database.
define("HOST", "hostname");
define("USER", "username");
define("PASS", "password");
define("DB", "dbname");
$link = mysqli_connect(HOST, USER, PASS) or die("Couldn't make database connection.");
$db = mysqli_select_db($link, DB) or die("Couldn't select database");
Strange things is that when I delete or die("Couldn't make database connection.") and or die("Couldn't select database"); then it's connect to db. Why ? Is there anything I'm doing wrong ?
This is the proposed way to handle the situation:
if (mysqli_connect_error()) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}

Error connecting PHP to SQLServer

I am using a script php that connect to sqlserver but it doesn't works.
//connection to the database
$dbconn = mssql_connect($Server)
or die("Couldn't connect to SQL Server on $Server");
if($dbconn)echo 'Connected';
//select a database to work with
$selected = mssql_select_db($DB, $dbconn)
or die("Couldn't open database $myDB");
![when running script it gives this error, i m using windows authentication][1]
http://i.stack.imgur.com/a9ndN.jpg
Check mysql_connect()
$dbconn = mysql_connect('hostname','username','password');
Connect using mysql_connect()
$dbconn =mysql_connect(servername,username,password);
example
$con = mysql_connect("localhost","me","123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql not connect with php scripts?

<?php
$cons=mysql_connect("localhost","root","");
mysql_select_db("infogallery") or die('Not Connected to the data base:'.mysql_error());
?>
I write above code for connection with mysql but when i run this scripts..nothing display on the brouser...what can i do for the connection with mysql....
If nothing is displayed, then it means it succeeded. Add more code which queries the database and displays some results.
Don't connect as the root account. Create an account specifically for playing around with.
Once you've done that, modify your code as follows:
$cons = mysql_connect('localhost', 'username', 'password');
if ($cons === FALSE) {
die("Failed to connect to MySQL: " . mysql_error());
}
mysql_select_db(etc.....);
You don't check if the connection failed, then try to do a database operation on that potentially failed connection. The or die(...) you have will only show the error caused by the select attempt, and the error message from the failed connection will be lost.
I like to just do
mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("infogallery") or die(mysql_error());
echo "So far, so good.";
How about something like the following:
<?php
try {
$cons = mysql_connect("localhost","username","password");
} catch ($e) {
die('Failed to connect to the database: ' . mysql_error());
}
try {
mysql_select_db("infogallery", $cons);
} catch ($e) {
die('Failed to select the database: ' . mysql_error());
}
?>

Categories