0

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
user968270
  • 4,251
  • 5
  • 21
  • 20

1 Answers1

-1

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

Chris Sprague
  • 368
  • 1
  • 4
  • 12
  • The above works in the command line and puts me in the mySQL CLI. – user968270 Jan 11 '17 at 00:10
  • when in there (mysql cli) issue `> use myToDo;` to see if your DB is properly setup and accessible. If properly setup, you may still need to grant access privileges to `user` – YvesLeBorg Jan 11 '17 at 01:09
  • The user in this case is the root user: issuing the command above gets me "Database changed" – user968270 Jan 11 '17 at 01:55
  • @user968270 i had not seen your comment above on debian ... what versions of php and mysql are you using ? – YvesLeBorg Jan 11 '17 at 02:34
  • php 7.0, mySQL 5.5.53 – user968270 Jan 11 '17 at 02:44
  • with php5, in order to get the pdo driver, i had to install php5-mysql with `apt-get` ... look for the equivalent for your php7, however and wherever you got the packages from. It does not seem to be bound to your php, which you could confirm on cli `php -m` will list the currently installed and running modules. – YvesLeBorg Jan 11 '17 at 03:04
  • I found **[this link](https://www.cyberciti.biz/faq/installing-php-7-on-debian-linux-8-jessie-wheezy-using-apt-get/)** which may be of help for configuring/installing php7 on jessie. – YvesLeBorg Jan 11 '17 at 03:13