Error message displaying after execution of a query![]()
In our student table there is no field as new_field. Here here is the message we will get
Note that mysql_error() is deprecated as of PHP 5.5.0 so better to avoid using this function. So if you are using PDO then we can use errorInfo() to display the returned error message from MySQL
Here is a sample code in PHP using PDO to display record and on failure to display error message.
In the above code there is an error in sql , there is no column by name no_name. The output will be
So to get correct result change the sql part like this .
With this you will get desired output.
Handling PDO errorsIf such error occurs what is to be done ? We have three options to handle in case of errors.We can stop the execution of the script. ( Fatal error : stop execution of code ) We can display warning message ( Warning only, display message and no stoppage of execution ) Remain silent ( continue to execute and display error message if required ) Setting the PDO error handling attribute.We can use setAttribute to tell how to handle the PDO errors.Here is a sample
Here is the complete code.
Now we have given all the three options but commented two and set the attribute to Silent. You can change the commented status and see how the script is behaving in different setAttribute values.
It is clear that the above code will generate error message. We can store the error message in a database or we can post ( by mail ) to the programmer about these details. Store the error message generated in an sql query or send email >> Subscribe to our YouTube Channel here
This article is written by plus2net.com team.
https://www.plus2net.com
![]() ![]() | ||||||||
| ||||||||