0

I'm trying to connect to my SQL database and I'm getting the error mentioned above.

This is the PHP code

// This file has database info variables (password etc.)
include "db_config.php";

class DB_CONNECT
{
function __construct()
{
    $this->connect();
}

function __destruct(){}

function connect()
{
    $connect = mysql_connect($db_server, $db_user, $db_password) or die();
    $db = mysql_select_db($db_name) or die();
    echo "Connection successful!";
    return $connect;
}
}

// Just to try out if the connection is successful
new DB_CONNECT();

This is the error

Warning: mysql_connect(): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) in /hermes/bosnaweb05a/b1/ipg.matejhacincom1/dbtest/db_connect.php on line 16

The funny thing is that I can connect to the database easily with this code which is generated for db testing by my hosting provider:

$link = mysql_connect('my db server', 'db user', 'db pass'); 
if (!$link) { 
die('Could not connect: ' . mysql_error()); 
} 
echo 'Connected successfully'; 
mysql_select_db(my db table); 

Could someone let me know what I'm doing wrong? I have no idea anymore.

The reason I mentioned that I'm not the server adming is because I saw a lot of questions like this here, but all of them have instructions how to solve it by using some server commands etc.

Guy
  • 6,414
  • 19
  • 66
  • 136
  • **Warning**: You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). – Quentin Feb 08 '15 at 10:17
  • Thank you very much! I was not aware of this. – Guy Feb 08 '15 at 10:23

4 Answers4

0

Either:

  • There isn't a MySQL server running on the same machine as the webserver or
  • The MySQL server is configured to place its socket file somewhere other than /var/run/mysqld/mysqld.sock (in which case you need to specify where it is or
  • The MySQL server is configured to listen only on TCP/IP (which is inefficient but it can be accessed by using the 127.0.0.1 IP address instead of localhost).
Community
  • 1
  • 1
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

Probably in variables $db_server, $db_user, $db_password you have wrong data. You must pass to object data from db_config.php file. For example through constructor.

Piotr Olaszewski
  • 6,017
  • 5
  • 38
  • 65
  • There is no way that supplying the wrong username or password could produce that error. – Quentin Feb 08 '15 at 10:14
  • Of course it can produce error, because when Whiz connect outside of the method class it's working. – Piotr Olaszewski Feb 08 '15 at 11:00
  • No, it has to be a problem with the connection string because the error message says that the connection failed, not that the login was rejected. – Quentin Feb 08 '15 at 11:01
0

Have the same problem on an customer server, last week, then :

  • Be sure your $db_server is according your hosting ; not a remote server.
  • Then use 127.0.0.1 instead of localhost

In my case, the MySQL server was different the PHP.

Akarun
  • 3,220
  • 2
  • 24
  • 25
-1

Thank you for all of your answers but what solved my problem was nothing that was in any of your answers.

All I did was used PDO instead of deprecated mysql and it worked. I also for some reason needed to move inclulde "db_config.php" in to the class because variables didn't work otherwise.

Anyway, here's the code that now works:

class DB_CONNECT
{
function __construct()
{
    $this->connect();
}

function __destruct(){}

function connect()
{
    include "db_config.php";
    $connect = new PDO($db_server, $db_user, $db_password) or die();
    echo "Connection successful!";
    return $connect;
}
}

// Just to try out if the connection is successful
new DB_CONNECT();
Guy
  • 6,414
  • 19
  • 66
  • 136