I have a file on my server called test.php. Here are the contents:
<?php
echo "hi\n";
try {
$dbh = new PDO('mysql:host=localhost;dbname=db', "foo", "bar");
echo "hi\n";
} catch (PDOException $e) {
die("Error!");
}
echo "hi\n";
?>
The code is fairly straightforward. It should print hi three times if everything works out.
When I run this code from the shell, it does indeed print “hi” on three separate lines. However, when I access this file through the website (http://localhost/test.php), it only prints one hi.
Removing $dbh = new PDO(‘mysql:host=localhost;dbname=db’, “foo”, “bar”); causes all three his to print successfully via website.
The MySQL information is correct and the user does have access to the database.
What would cause me accessing the file directly through the shell to print all three times, but only print once when accessed via the website?
Are you sure you have enabled PDO extension? If you get only one “hi” it means that you get fatel error in your PDO object declaration. Something like this
"Fatal error: Class 'PDO' not found"Please check.http://php.net/manual/en/pdo.installation.php
Your code works fine on me anyway.