1
votes

I'm having trouble connection to the database

Here is the code I am using

$con = mysql_connect('host', 'user', 'pass');
mysql_select_db('database_name', $con);

And here is the results I get

Warning: mysql_connect() [function.mysql-connect]: Lost connection to MySQL server at 'reading initial communication packet', system error: 111 in /home/heartbeat_db/heartbeatsmart.com/php/config/dbconnect.php on line 2

Warning: mysql_select_db() expects parameter 2 to be resource, boolean given in /home/heartbeat_db/heartbeatsmart.com/php/config/dbconnect.php on line 3

Warning: mysql_connect() [function.mysql-connect]: Lost connection to MySQL server at 'reading initial communication packet', system error: 111 in /home/heartbeat_db/heartbeatsmart.com/php/config/dbconnect.php on line 2

Warning: mysql_select_db() expects parameter 2 to be resource, boolean given in /home/heartbeat_db/heartbeatsmart.com/php/config/dbconnect.php on line 3

5
your code is deprecated. - syrkull
Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial. - cryptic ツ
I was getting ready to switch, and I have an even better reason now. The new host only accepts mysqli. - user1661548

5 Answers

1
votes

It's better to use PDO or Mysqli. I prefer PDO because it also supports other databases than mysql so you can easier migrate if necessary.

You can easily make a connection through

$db = new PDO('mysql:host=localhost;dbname=<SOMEDB>', '<USERNAME>', 'PASSWORD');

For more information: http://php.net/manual/en/book.pdo.php

If you want to use mysqli use:

$mysqli = new mysqli("localhost", "user", "password", "database");
3
votes

try using this code

$con=mysql_connect("host","user","pass");
mysql_selectdb("database_name",$con);

0
votes

Try using this code

$link = mysqli_connect('localhost', 'my_user', 'my_password', 'my_db');

if (!$link) {
    die('Connect Error (' . mysqli_connect_errno() . ') '
            . mysqli_connect_error());
}

echo 'Success... ' . mysqli_get_host_info($link) . "\n";

mysqli_close($link);

Source: http://www.php.net/manual/en/mysqli.construct.php

0
votes
<?php
$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " .      $mysqli->connect_error;
 }
echo $mysqli->host_info . "\n";

$mysqli = new mysqli("127.0.0.1", "user", "password", "database", 3306);
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " .      $mysqli->connect_error;
}

echo $mysqli->host_info . "\n";
?>
0
votes
$con = mysql_connect('host', 'user', 'password');
if (!$con) {
    die('Not connected : ' . mysql_error());
}

$db = mysql_select_db('database_name', $con);
if (!$db) {
    die ('Can\'t use database_name : ' . mysql_error());
}