0

I'm doing the Laracasts for PHP Practitioner and can't connect to my local SQL database using a PDO PHP function.

I've reviewed all of the settings in MySQL Workbench for the database. The database name in the code is correct. The port is correct. The local host IP is correct.

try {
    $pdo = new PDO('mysql:host 127.0.0.1:3306;dbname=Test', 'root', '');
} catch (PDOException $e) {
    die('Could Not Connect');
};

I expected this to work and to be able to pull information from my SQL database tables. However, it continues to throw an exception and only give me my die command prompt.

I've also confirmed the MySQL server is running via MySQL workbench.

What needs to change in order for this command to properly connect?

Boann
  • 48,794
  • 16
  • 117
  • 146
Ryan
  • 9
  • 3
  • For all of us who don't know, here is the [Laracasts for PHP Practitioner](https://laracasts.com/series/php-for-beginners). – KIKO Software Jun 22 '19 at 15:37
  • I think you forgot `=` between `host` and IP address. – alx Jun 22 '19 at 15:37
  • `host=127.0.0.1` Aslo, `$e` has `getMessage` which shows you what exactly went wrong. – u_mulder Jun 22 '19 at 15:38
  • added the = but doesn't change the result. – Ryan Jun 22 '19 at 15:39
  • 3
    Try looking at what the exception says instead of ignoring it: `die($e->getMessage());` – Boann Jun 22 '19 at 15:39
  • This is how you are supposed to connect to PDO: https://phpdelusions.net/pdo#dsn – Dharman Jun 22 '19 at 15:40
  • Possible duplicate of [PDO Connection string: What is the best way to do it?](https://stackoverflow.com/questions/56576830/pdo-connection-string-what-is-the-best-way-to-do-it) – Dharman Jun 22 '19 at 15:40
  • super helpful. actual error is "SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client" – Ryan Jun 22 '19 at 15:44

1 Answers1

3

For starters, you should examine the actual error. Your "Could Not Connect" message doesn't help you or us diagnose the problem, but the actual exception thrown would. Ideally, you would have more precise error-handling code. At a minimum, though, you could var_dump($e) inside your catch block.

That said, your connection string (DSN) syntax is wrong; you're missing an = and are specifying the port incorrectly.

$pdo = new PDO('mysql:host=127.0.0.1;port=3306;dbname=Test', 'root', '');
//                        ^         ^ you need ;port=, not just a colon
//                        |
//                        should be =, not a space

A couple of other notes:

  • It's not clear from your example whether you have a password on your root account. If so, great, and please do not post it here. If you do not have a password on that account, set one immediately.
  • You really should not be connecting as root, anyway. Set up a different user account.
  • You do not need the ; after the final }.
elixenide
  • 44,308
  • 16
  • 74
  • 100
  • 1
    if this is the case (solution-wise), should this question not be closed as a typo-issue? – treyBake Jun 22 '19 at 15:44
  • @treyBake Questions should be closed as typos only if the problem has actually been solved without requiring an answer (*e.g.*, in the comments, because OP noticed their own typo, etc.). If it requires an answer to explain the typo, then just closing the question without explaining the issue to OP is not very helpful to anyone. – elixenide Jun 22 '19 at 15:47
  • 1
    I don't think so.. `While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting.` - anything not useful to future readers is also under this banner, which this question isn't IMO – treyBake Jun 22 '19 at 15:47
  • Thanks, getting the die command to throw the actual error helped. Used this post to solve the mySQL authentication issue: https://stackoverflow.com/questions/50026939/php-mysqli-connect-authentication-method-unknown-to-the-client-caching-sha2-pa – Ryan Jun 22 '19 at 15:48
  • 1
    [Don't die](https://stackoverflow.com/a/15320411/1839439) in your code. Use proper exceptions or leave the errors be as they are. – Dharman Jun 22 '19 at 15:49