0

I'm using PDO for connect my DB with my form HTML for create advanced registration.

The problem is that I receive this error message:

Erreur: SQLSTATE[HY000] [1044] Access denied for user 'mysuser'@'%' to database 'advanced-registration'

I check the login info, everything is ok.

The database is hosted by OVH.

My code for connection:

try {

  $db = new PDO('mysql:host=myhost; dbname=advanced-registration','myuser', 'mypass');
  $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

  $db = null;
  } catch (PDOException $e) {
echo "Erreur: ".$e->getMessage()."<br/>";
  die();
 }
Isaac Bennetch
  • 11,830
  • 2
  • 32
  • 43
Pierre Dumay
  • 33
  • 2
  • 7
  • What about the user rights? – Michael Jun 06 '18 at 18:42
  • `mysql:host=myhost; dbname=advanced-registration'` There can be NO spaces in the DSN string. PDO is incorrectly parsing it, stopping after `myhost` and the connected user account has no privileges for the default database. It must be as `mysql:host=myhost;dbname=advanced-registration'` and I am unsure how the hyphen in `advanced-registration` will be treated. – Michael Berkowski Jun 06 '18 at 18:47
  • "Pas de privilèges" à cause de cela? @Michael – Pierre Dumay Jun 06 '18 at 18:47
  • Because PDO ignores dbname entirely, unusual things can happen. The user you're connecting with may have no grants except on the intended schema. – Michael Berkowski Jun 06 '18 at 18:50
  • I tested a database with a hyphenated name like `a-b` and the DSN will accept that without difficulty, though in any other MySQL context you will need to backtick quote it `\`advanced-registration\`` – Michael Berkowski Jun 06 '18 at 18:55
  • @MichaelBerkowski I also test without hyphen and always the same error `registration` – Pierre Dumay Jun 06 '18 at 18:58
  • If you database is named `advanced-registration` then that is fine. It is the _space_ in your PDO DSN string at fault. You must use `'mysql:host=myhost;dbname=advanced-registration'` without whitespace. – Michael Berkowski Jun 06 '18 at 19:06
  • @MichaelBerkowski It's fix too ... – Pierre Dumay Jun 06 '18 at 19:11

0 Answers0