PDO connection error "Failed: could not find driver"

Hi all,

I can use PDO to connect local mysql database successfully , but when i try to use ODBC (driver 5.1) to connect remote database , i have an error code Failed: could not find driver

my php code as below , please advise , thank you

[php]<?php
try
{
$dsn = “odbc:DRIVER={MySQL ODBC 5.1 Driver};host=192.168.79.10;dbname=aaa”;
$dbh = new PDO($dsn, “testuser”, “88888888”);
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, TRUE);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// All other database calls go here
}
catch (PDOException $e)
{
echo "Failed: ". $e->getMessage();
}
$sql = “select * from account’
order by callednum”;
// Execute the statement and echo the results
$results = $dbh->query($sql);
foreach ($results as $row)
{
echo "









$row[account] $row[callednum] $row[finalDestination] $row[countryId] $row[country]
";
}
?>[/php]

Brgds/Brandon Chau

Do a phpinfo() line and see if you have the requested driver installed.

The correct syntax is

$pdo = new PDO(‘mysql:host=server;dbname=databasename’, ‘username’, ‘password’);

Hi astonecipher,

I just see a blank page if i changed to

$pdo = new PDO(‘mysql:host=server;dbname=databasename’, ‘username’, ‘password’);

i have captured a .jpg about info() (attached)

Can you see something ? Thanks.

Brgds/Brandon Chau


PDO drivers enabled: mysql, sqlite

You need to get your host to enable the driver you want.

Hi JimL,

I have a bit confusing , i can get the result when i’m using Mysql query browser directly, but when via WAMP getting blank page; Is it something need to set on WAMP’s Apache server?

Thanks

Brgds/Brandon Chau

Do a var_dump on $row and see if you are returning the results.

Hi astonecipher,

Still blank , so i think no any result returning back

Thanks

Brgds/Brandon Chau

You have an errant ’ in your posted sql statement.

Hi astonecipher,

Sorry , i didn’t check after copy and paste :stuck_out_tongue:

Actually , my original code is correct, as i said , i can returning back the result with same code when using local db server
(like that)
$dsn = “mysql:host=localhost;dbname=aaa”;
$dbh = new PDO($dsn, “brandonsan”, “88888888”);

but after i changed the host to remote db server getting blank page
$dsn = “mysql:host=192.168.79.10;dbname=aaa”;
$dbh = new PDO($dsn, “brandonsan”, “88888888”);

(P.S i’m using the ODBC 5.1 driver to connect the remote db server 192.168.79.10) So i’m not sure how to config the $dsn

Thank you

Brgds/Brandon Chau

So it works when it is localhost, but not with the 192 ip address?

Hi astonecipher,

Yes, because localhost isn’t using odbc to connect , so it works.

But remote host need to use odbc to connect

ty

Brgds/Brandon Chau

Sponsor our Newsletter | Privacy Policy | Terms of Service