Eugene
Eugene

Reputation: 343

MySQL error on trying to open a MySQL connection

I'm getting the following error when I try to open up a connection with MySQL running on my MACBOOK:

Warning: mysql_connect(): [2002] No such file or directory (trying to connect via unix:///var/mysql/mysql.sock) in /Users/Eugene/Sites/website/includes/database.php on line 15 Warning: mysql_connect(): No such file or directory in /Users/Eugene/Sites/website/includes/database.php on line 15 Notice: Undefined variable: connection in /Users/Evgeny/Sites/website/includes/database.php on line 16 Database connection failed: No such file or directory

My constants are:

defined('DB_SERVER') ? null : define("DB_SERVER", "localhost");
defined('DB_USER') ? null : define("DB_USER", "eugene");
defined('DB_PASS') ? null : define("DB_PASS", "password"); // no comments please :)
defined('DB_NAME') ? null : define("DB_NAME", "maindb");

The way I try to connect is:

$connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS);
if(!$connection) // if no connection - die
{
    die("Database connection failed: " . mysql_error());
}
else // if there is a connection, then go and select a database
{
    $db_select = mysql_select_db(DB_NAME, $connection);
    if(!$db_select) // if no database selected - die
    {
        die("Database selection failed: " . mysql_error());
    }
}

Please help,

Thanks in advance.

Upvotes: 0

Views: 513

Answers (4)

wyknzo
wyknzo

Reputation: 81

If u'are calling $connection from a class just put if(!$this->connection){.....} or if not just put this line if (!$connection){.....}

Upvotes: 0

symcbean
symcbean

Reputation: 48357

$this->connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS);

First problem is that mysqld is not using the same socket path as the client. Try accessing via 127.0.0.1 then run 'show variables' to find it where it has put the socket - update your PHP config appropriately.

if(!$connection)

But you put the connection in '$this->connection' not '$connection'

Upvotes: 2

Konrad Neuwirth
Konrad Neuwirth

Reputation: 898

You're mixing class variables and local variables. $this->connection is not the same as $connection.

Upvotes: 0

futureal
futureal

Reputation: 3045

You are setting an instance variable via $this but testing a local variable. Assuming no other changes, your code should look like:

$connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS);
if (!$connection) // if no connection - die
{
    die("Database connection failed: " . mysql_error());
}

Give that a try!

Upvotes: 0

Related Questions