Page 1 of 1

PDO driver error - but the driver is there

Posted: Thu May 13, 2010 9:25 am
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

Re: PDO driver error - but the driver is there

Posted: Thu May 13, 2010 10:37 am
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");

Re: PDO driver error - but the driver is there

Posted: Thu May 13, 2010 12:39 pm
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.

Re: PDO driver error - but the driver is there

Posted: Thu May 13, 2010 12:56 pm
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:

Re: PDO driver error - but the driver is there

Posted: Fri May 14, 2010 11:59 am
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!

Re: PDO driver error - but the driver is there

Posted: Fri May 14, 2010 12:09 pm
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)

Re: PDO driver error - but the driver is there

Posted: Fri May 14, 2010 12:10 pm
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.

Re: PDO driver error - but the driver is there

Posted: Tue May 18, 2010 4:40 am
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