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 PHP & Databases with PDO Getting Started With PDO Managing Errors

Adam Menczykowski
Adam Menczykowski
2,319 Points

When trying to break the code in the workspace (by changing the database name) the output does not change.

object(PDO)#1 (0) { } is the result even when having the wrong database name.

thank you

5 Answers

Tomasz Budny
Tomasz Budny
14,588 Points

I don't know if you noticed, but now when you'll pass in to the PDO statement a database file name that does not exist, Workspaces will create that file for you automatically. Just refresh the file view on the left side and you'll see it appear. So it does not return an error because it avoids it by making the file for you.

Just make a different syntax error e.g.

$db = new PDO('sqliteee:./database.db');

Lei Zhou
Lei Zhou
2,445 Points

you r right!

kabir k
kabir k
Courses Plus Student 18,036 Points

So, why does the teacher's code behave as expected and ours don't? Even though, it's the same code in the same workspace.

Tomasz Budny
Tomasz Budny
14,588 Points

I don't know this for sure but I'm guessing that It might be a new feature added with a database software update or a matter of settings.

kabir k
kabir k
Courses Plus Student 18,036 Points

Ok thanks, Tomasz. I was thinking it was something along that line as well but I wasn't sure either.

tzehuilee
tzehuilee
19,911 Points

The deliberate misname of database for the PDO only creates new file instead of error message indicating the file does not exist. I thought there is something is wrong with Trehouse launchable workspace. I tried to use Netbeans to run the code. Same thing happened. I also manually type localhost/index.php. Again, it's still the same result. I wonder what version PHP did Mr. Paulk used for this course. I am also wondering how he did configure his PHP files especially the php.ini file. This part of course may need a fix soon.

Adam Menczykowski
Adam Menczykowski
2,319 Points

This seems to be a problem with the Treehouse workspace not effectively saving my code and displaying the preview that reflects the code.

Adam Menczykowski: I have the same problem that the PDO object is still returning, but I can confirm that the workspace is indeed saving your code to then preview it.

If you mistype PDO it'll throw the usual error associated with incorrect classes being defined.

Try catching PDOException instead & attempt to do a method associated with an instance of a PDO object. For the sake of clarity, remove die() from the first clause temporarily, but doing that shouldn't help fix anything since the code should be throwing before PHP can execute die().

With my PHP skills admittedly being rusty, It may very well be the fact the exception we should be catching is not specific enough or a new way of dealing with failed PDO objects has materialized with newer versions of PHP I'm not accustomed to.

Hampton Paulk, I'm sure will address this as soon as he is able to.

Thanks Adam I completed that code