1

I don't know why, but using PDO MYSQL when trying to connect with:

$host='localhost';
$db='contratos';
$user='contratos';
$password='!!contratos';
$link = new \PDO("mysql:host={$host};dbname={$db};charset=utf8", $user, $password);

PDO tries to connect adding an extra @ before host:

PDOException: SQLSTATE[HY000] [1045] Accès refusé pour l'utilisateur: 'contratos'@'@localhost' (mot de passe: OUI) in C:\wamp\www\contratos\classes\mysql.php on line 154

I'm using a external class to handle all mysql queries, but, before it was working properly and none of my code was modified.

Is this a PHP, MYSQL, PDO or what error/bug?

edit: I'm trying to connect, not to create user or grant any rights, I can't connect through PHP, but I'm able to connect using MySQL Workbench

  • 1
    Possible duplicate of [MySQL: Grant \*\*all\*\* privileges on database](http://stackoverflow.com/questions/5016505/mysql-grant-all-privileges-on-database) – Machavity Oct 13 '15 at 15:16
  • 1
    Considering you aren't setting the error mode to exceptions in this block, is this really line 154 in your mysql.php class? – Devon Bessemer Oct 13 '15 at 15:28

2 Answers2

2

the char '@' in the error message is a translation error of PDO, for the fr_FR locale. So you need to ignore this obsolete char and investigate about your mysql user's permissions.

AndreaPosadino
  • 188
  • 1
  • 7
  • 1
    Thank you, the user@localhost was really deleted, this was the problem, and I was even able to change the language of errors in MySQL. – Gabriel Rodrigues Oct 14 '15 at 12:32
-2

This works for me:

$link = new \PDO("mysql:dbname=$db;host=$host", $user, $password, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
Vincent
  • 2,963
  • 1
  • 19
  • 26
Snoozer
  • 585
  • 1
  • 8
  • 16