I believe that Stack Overflow is very useful repository of information for students and professionals. When we answer we should strive for best quality, so anyone reading the answer can benefit from it. It does not necessarily need to be production-ready code, but we should strive for correct answers and avoid teaching bad habits.
The reason for my comment on your post is very well explained by @YourCommonSense in the linked post.
So, never use die() with mysql errors, even for the temporary debugging: there are better ways.
Your suggested method for debugging was to insert or die($conn->error)
at the end of the line. This will be executed if the prepare
function fails due to some errors and it will display the error message to the end-user. This is not error checking, it is error displaying.
You haven't made it clear in your post that this piece of code was added only for debugging purposes. To the OP and whoever else might stumble upon this post, it looks like part of the code logic. They might not know that it is only to display error while coding the logic and that it is there only temporarily.
You are correct saying that I do not put "error checking" in my answers. PHP has functionality of generating errors on its own. If the code fails an error is generated, which will either be logged into error log file or displayed to the user.
error_reporting(E_ALL); // <- enable error generation
ini_set("log_errors", 1); // <- enable error logging to a file
ini_set('display_errors', 1); // <- enable error display
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); // <- enable errors for MySQLi extension
These settings should be enabled in Development, but display_errors
should be switched off in Production.
Taking advantage of PHP's standard error reporting you do not need to use die()
to debug your code. If your environment is properly configured you will see the errors. If the standard error reporting is not enough, you can avail of PHP's function to generate user errors called trigger_error
which will respect your environment's INI settings.
Killing PHP code using die()
is very nasty habit, which we should try to avoid in our answers.
The vast majority of questions on SO come from students doing exercises, not writing professional code.
-- That is a very sad statement to make. Even if it were true, students should aspire to become professionals.