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);
    $DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    # 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);
}
Member Avatar for diafol

Perhpas you need to place the execute line:

$DBH->execute();

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

Diafol:
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 =
object(PDO)[1]
$dbname =
string 'test' (length=4)
$e =
Undefined
$host =
string 'localhost' (length=9)
$pass =
string 'xxxxxx' (length=6)
$user =
string 'root' (length=4)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.