Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Using PHP with MySQL Querying the Database with PHP Querying the Database

Mike Baxter
Mike Baxter
4,442 Points

MySQL Exception: From MAMP, MySQL database, or video's code?

Hey folks,

In the "Querying the Database" lesson of "PHP + MySQL", I tried the code exactly as typed, and I'm getting an exception thrown by the following line:

$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

I comment the line out and it works fine. I know the code is "correct" because I Googled it and copied the code from several different sources after typing it exactly like the video had it. (So I'm sure it's not a typo.)

I'm running PHP and MySQL through MAMP. Any idea why this might be happening? Has anyone else had the same issue? My guess is there's some arcane setting or permission that needs adjustment.

Thanks!

Steve Wamsley
Steve Wamsley
7,977 Points

What is the exception message? And can you copy the rest of the code surrounding this call.

1 Answer

Mike Baxter
Mike Baxter
4,442 Points

Oh wow, I feel dumb now. I typed "utf-8" instead of "utf8." The exception thrown was

SQLSTATE[42000]: Syntax error or access violation: 1115 Unknown character set: 'utf-8'

From there it was obvious. Thanks Steve Wamsley, I guess I need to get into the practice of using and understanding exceptions. (Maybe that's in further PHP tutorials on Treehouse?)

For anyone who comes across this thread in the future and wants to check exceptions, just do the following:

try {
}
catch (Exception $e) {
    echo $e->getMessage();
    exit;
}
Steve Wamsley
Steve Wamsley
7,977 Points

Glad I could help! Not sure where exceptions are covered in the material, but it looks like you figured it out.

Mike Baxter
Mike Baxter
4,442 Points

Steve, I haven't seen it yet, and I've done the "Build a Simple PHP Website" and "Using PHP with MySQL" projects. Maybe they mention it in the "Enhancing a Simple PHP Application" project?