PDO always throws exception [duplicate]

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

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:

<?php

try {
  $pdo = new PDO('mysql:host=127.0.0.1;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.

EDIT: ISSUE RESOLVED

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

-1
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 127.0.0.1 myToDo -uuser -p

Answer this questsion