PHP connects to MySQL but Apache cannot

I have written a basic MySQL database connection code in PHP (5.5), which runs fine as a PHP script but the same script when run from Apache(2.4) gives the error “Connection failed: could not find driver”

try {
$conn = new PDO(“mysql:host=$servername;dbname=db”, “username”, “password”);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo “Connected successfully using PDO”;
}catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}

Check your install.

phpinfo();

Brings up your install information. You want to look at the PDO section:
PDO
PDO support enabled
PDO drivers sqlite, mysql, pgsql, odbc

Sponsor our Newsletter | Privacy Policy | Terms of Service