well…I have the following problem:
I am using backbone to handle js code…and one of its function handles the response from the server.
I want to perform some action based on this response…but when a prepared statement fails and for example I get this warning:
<b>Warning</b>: mysqli_stmt::bind_param(): Number of elements in type definition string doesn't match number of bind variables in <b>C:\Apache24\htdocs\Appointments\Administrator\admin_db_code.php</b> on line <b>246</b><br />
I cannot check for the above warning…the server does return the above but it is useless in the client.
Normally I use json_encode but in this case it will do nothing.
Since I will send the error in a log,I will not print it to the browser.
I have no idea how logging works…how I could send the above error related to stmt in a log. I know this is a huge topic, I would appreciate though if you could help a little
PDOException does not work on MySQLi. you’d need 1) set MySQLi to use exceptions (hint: see mysqli_driver) 2) catch a mysqli_sql_exception or RuntimeException
that depends how much error handling code you want to write. I personally prefer exceptions because they have some important advantages over return values.
the only thing you really need is error handling as such. whether you use return values, errors, or exceptions for that depends on your personal laziness.
What are the important advantages of exceptions-as you mention?
I don not make this question just out of curiosity…in the beginning of this topic you see the error checking code I use for the prepared statement.
I am afraid that with the above approach some errors are not detected.
exceptions travel the call stack. i.e. you can handle the problem at a position in your code, where it is most appropriate. esp. with output this is usually far away from where those errors occur (a database failure can thus be handled in the output section). additionally you don’t have to take care of exception propagation (which you would have to do if you would only consider return values)
exceptions have lots of different types. there are over a dozen built-in exception classes and you can build your own as well. therefore it is dead easy to distinguish between a database exception and an exception thrown due to invalid input.
exceptions carry a trace, that is a list of every function/method with their arguments and the position in that function that the exception went through before it was caught.
some built-in functions/classes can throw exceptions automatically (PDO, MySQLi, DateTime) so you don’t need to throw them yourself.
I got your point…since this topic is about SQL error checking(I am using MySQLi) than you might agree that return values might be OK.-in the future I intend setting more elaborate error-checking mechanisms.
And that exception would be helpful in other type of PHP code-not related to SQL statements
I got your point…since this topic is about SQL error checking(I am using MySQLi) than you might agree that return values might be OK.-in the future I intend setting more elaborate error-checking mechanisms.
And that exception would be helpful in other type of PHP code-not related to SQL statements