I was taking a tutorial on PDO with PHP and MySQL and noticed I could not generate an exception from this code. I wonder what setting I need to put in place to generate the exception.
If I shut down MySQL I can generate an error but with MySQL running the error is not generated. Below is the error.
SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it.
Any ideas of what I should be looking for.

# connect to the database
try {
    $DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);

    # UH-OH! Typed DELECT instead of SELECT!
    $DBH->prepare('DELECT * FROM comments');
} catch (PDOException $e) {
    echo "I'm sorry, Dave. I'm afraid I can't do that.";
    file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);
3 Years
Discussion Span
Last Post by rouse

Perhpas you need to place the execute line:


before the catch block?
Turning off MySQL is probably giving you a connection exception, nothing to do with the query.


When I try you suggestion, I still don't get to the catch block, but I get a full blown page from PHP detailing my error and location in the stack. This makes me wonder if I need to change some setting in my php.ini file.

( ! ) Fatal error: Call to undefined method PDO::execute() in C:\NetBeansWorkPlace\webWork\index_PDO_OO2.php on line 20
Call Stack
#   Time    Memory  Function    Location
1   0.0189  134848  {main}( )   ..\index_PDO_OO2.php:0
Variables in local scope (#1)
$DBH =
$dbname =
string 'test' (length=4)
$e =
$host =
string 'localhost' (length=9)
$pass =
string 'xxxxxx' (length=6)
$user =
string 'root' (length=4)
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.