halfer / php-tutorial-text

The chapter text for the "I ♥ PHP" project
14 stars 1 forks source link

Minor bug with how PDO error is handled #24

Closed NikolaKo closed 6 years ago

NikolaKo commented 7 years ago

Hi,

I think I've spotted a minor bug in chapter 5 (https://ilovephp.jondh.me.uk/en/tutorial/make-your-own-blog/commenting-form?version=v5), when "addCommentToPost" function is first introduced this method "$pdo->errorInfo()" is used to get potential error if SQL query fails. I've noticed that my SQL queries were failing silently without any errors being caught so after a bit of investigation I found this: http://php.net/manual/en/pdo.errorinfo.php and according to that documentation :

If you create a PDOStatement object through PDO::prepare() or PDO::query() and invoke an error on the statement handle, PDO::errorInfo() will not reflect the error from the statement handle. You must call PDOStatement::errorInfo() to return the error information for an operation performed on a particular statement handle.

I changed line 81 from "$errorInfo = $pdo->errorInfo();" to -> "$errorInfo = $stmt->errorInfo();" and that solved the problem for me.

Hopefully this helps.

Kind regards.

halfer commented 7 years ago

Thanks @NikolaKo, good catch. I guess this isn't very well tested, since I didn't have a database error to check it against! I will make a note to update the code at some point soon.

How are you getting on with the tutorial generally? :smile_cat:

NikolaKo commented 7 years ago

Hi, generally I like the tutorial so far and I appreciate the fact it has some conceptual differences compared to usual materials (usage of diffs, gradual improvements, real life bugs etc.) , although it's fair to say I've been programming using other languages for some time so I'm mostly here to dive into syntax and some language specific patterns so I can't speak from the beginners point of view.

Thanks again.

halfer commented 7 years ago

No worries @NikolaKo. One area of improvement (which would be more suitable for you than for complete PHP beginners) is to use a proper framework. If you want something lightweight that is easy to learn, have a look at Slim Framework.

halfer commented 6 years ago

This is fixed in the latest release, thanks again for the report.