PDO always throws exception [duplicate]

Asked at 2017-01-10 17:58:07Z
  • 5 Subscribers

This question already has an answer here:

OS: Debian 8 "Jesse", PHP 7.0, mySQL 5.5.53

As the title says, I've created a PDO object trying to demo the basic functionality to myself with locahost, and it always throws an exception regardless of whether or not the database request is correct or not. Here's the code, with user and password as stand-ins for the real data:


try {
  $pdo = new PDO('mysql:host=;dbname=myToDo', 'user', 'password');
catch (PDOException $e) {
  die('Could not connect.');


When I run this on a test server (port 8888), whether the dbname I feed it is right or wrong, it always throws the exception. My code looks identical to that demonstrated on the Laracast excepting that his profile has no password and mine does. But I've confirmed I'm entering the password exactly as I configured it in mySQL.


After following the advice in the comments, I got a new error message that I was able to use googlefu on and found that I needed to manually install the packages containing the pdo extension like so:

apt-get install php7.0-mysql

1 answers in total

Chris Sprague Posted at 2017-01-10 18:07:23Z

I'm guessing if you dump out the exception message as suggested it will say something like "connection refused" or "Access denied".

Try connecting to the database from the command-line just to verify it works outside of PDO:

$ mysql -h myToDo -uuser -p

Answer this questsion