PDO driver error - but the driver is there

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
steel_rose
Forum Newbie
Posts: 15
Joined: Thu May 13, 2010 9:17 am

PDO driver error - but the driver is there

Post by steel_rose »

Hi all,

I'm new to the forum (and relatively new to PHP) and I have already learned a lot from reading the other posts!
Thank you very much for all the knowledge that is shared here.

There is one problem I'm having that I can't find a solution to...

I'm trying to connect to a database, as long as I use mysqli everything is fine, but I wanted to experiment with PDO. Every time I try I get an error message telling me that the driver was not found, so I tried to find out which PDO drivers are installed, and mysql is supposed to be there.

Here is the code I'm running:

Code: Select all

 foreach (PDO::getAvailableDrivers() as $driver)
    {
        echo $driver . '<br />';
    }


$dbh = new PDO ('mysql : host = localhost ; dbname = cdcol', 'root', 'Broken00')
or die ("Error");
And here is the page I'm getting:

[text]
mysql
pgsql
sqlite
sqlite2

Fatal error: Uncaught exception 'PDOException' with message 'could not find driver' in /opt/lampp/htdocs/xampp/codershed.co.uk/dbtest.php:23 Stack trace: #0 /opt/lampp/htdocs/xampp/codershed.co.uk/dbtest.php(23): PDO->__construct('mysql : host = ...', 'root', 'Broken00') #1 {main} thrown in /opt/lampp/htdocs/xampp/codershed.co.uk/dbtest.php on line 23
[/text]

Anyone can understand what's happening?

Thank you sooo much...

SR
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: PDO driver error - but the driver is there

Post by mikosiko »

try this:

Code: Select all

$thehost = 'localhost';
$dbname = 'cdcol';
$username = 'root';
$userpass = 'Broken00';
$dbh = new PDO ("mysql:host = $thehost ; dbname = $dbname", $username, $userpass)
or die ("Error");
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: PDO driver error - but the driver is there

Post by Weirdan »

The documentation for mysql dsn (http://us2.php.net/manual/en/ref.pdo-my ... ection.php) does not suggest to use spaces in the dsn. Try removing those and see if it helps.
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: PDO driver error - but the driver is there

Post by mikosiko »

Weirdan wrote:The documentation for mysql dsn (http://us2.php.net/manual/en/ref.pdo-my ... ection.php) does not suggest to use spaces in the dsn. Try removing those and see if it helps.
absolutely correct Weirdan.... I din't see that :oops:
steel_rose
Forum Newbie
Posts: 15
Joined: Thu May 13, 2010 9:17 am

Re: PDO driver error - but the driver is there

Post by steel_rose »

Thanks for the advice and for the useful link!

I modified the code, but it's still not working.... but I get a completely different error message!

Code:

Code: Select all

<?php


 foreach (PDO::getAvailableDrivers() as $driver)
    {
        echo $driver . '<br />';
    }


$dbh = new PDO ('mysql:host=localhost;dbname=cdcol', 'root', 'Broken00')
or die ("Error");

?>
Result:
[text]
mysql
pgsql
sqlite
sqlite2

Warning: PDO::__construct() [pdo.--construct]: [2002] Invalid argument (trying to connect via unix://) in /opt/lampp/htdocs/xampp/codershed.co.uk/dbtest.php on line 23

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2002] Invalid argument' in /opt/lampp/htdocs/xampp/codershed.co.uk/dbtest.php:23 Stack trace: #0 /opt/lampp/htdocs/xampp/codershed.co.uk/dbtest.php(23): PDO->__construct('mysql:host=loca...', 'root', 'Broken00') #1 {main} thrown in /opt/lampp/htdocs/xampp/codershed.co.uk/dbtest.php on line 23
[/text]


If I use mikosiko's code (with or without spaces in the dns argument) I get the same exact error:

Code: Select all

$thehost = 'localhost';
$dbname = 'cdcol';
$username = 'root';
$userpass = 'Broken00';
$dbh = new PDO ("mysql:host = $thehost ; dbname = $dbname", $username, $userpass)
or die ("Error");

:(

I'm using LinuxMint with XAMPP if that's of any help... thanks for the patience!
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: PDO driver error - but the driver is there

Post by mikosiko »

Did you try my suggestion without the spaces as Weirdan suggest? (fixed below)

Code: Select all

$thehost = 'localhost';
$dbname = 'cdcol';
$username = 'root';
$userpass = 'Broken00';
$dbh = new PDO ("mysql:host=$thehost; dbname=$dbname", $username, $userpass)
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: PDO driver error - but the driver is there

Post by Weirdan »

It seems that it failed to connect to the server via unix socket (this is default behavior when you specify 'localhost'). Try with 127.0.0.1 instead.
steel_rose
Forum Newbie
Posts: 15
Joined: Thu May 13, 2010 9:17 am

Re: PDO driver error - but the driver is there

Post by steel_rose »

Weirdan wrote:It seems that it failed to connect to the server via unix socket (this is default behavior when you specify 'localhost'). Try with 127.0.0.1 instead.
Worked like a charm - Thank you Weirdan!!!

SR
Post Reply