PDO MySql Connection Help?

clanmsx2

New Member
Messages
1
Reaction score
0
Points
1
Hello, all! I am having a few troubles with my database connection...

The error I am getting:
Warning: mysql_real_escape_string(): Access denied for user 'clanmsx2'@'localhost' (using password: NO) in /home/clanmsx2/public_html/xxxx/xxxxxxx on line 5

Warning: mysql_real_escape_string(): A link to the server could not be established in /home/clanmsx2/public_html/xxxx/xxxxxxx on line 5

Warning: Cannot modify header information - headers already sent by (output started at /home/clanmsx2/public_html/xxxx/xxxxxxx:5) in /home/clanmsx2/public_html/xxxx/xxxxxxx on line 17

My Connection:
PHP:
$config['db'] = array(
    'host'      => 'localhost',
    'username'  => 'clanmsx2_xxxxxxxxx',
    'password'  => 'xxxxxxxxxxxxx',
    'dbname'    => 'clanmsx2_xxxxxxxxx'
);
try {
    $pdo = new PDO('mysql:host=' .$config['db']['host']. ';dbname=' .$config['db']['dbname'], $config['db']['username'], $config['db']['password']);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $pdo->exec("SET CHARACTER SET utf8");
} catch(PDOException $e) {
    exit(var_dump($e->getMessage()));
}
 

essellar

Community Advocate
Community Support
Messages
3,295
Reaction score
227
Points
63
mysql_real_escape_string() requires a mysql_connect() connection, and you haven't made one explicitly so the engine is trying to make one with your current credentials (your cPanel user name and no password).

The direct PDO equivalent is PDO->quote(), but using prepared statements and binding values does the necessary escaping automagically for you, and it's safer in general than using PDO->quote() and an immediate query.
 
Top