Log in

View Full Version : Best practices way to express simple queries?



kuau
08-03-2008, 10:28 PM
I am trying to learn best practices. Is it necessary or advisable to have error handling built into every statement, or is it OK to be more concise? Is there any advantage of one over the other of these 2 ways of doing the same thing? What happens if you remove the error handling and there is an error? Thanks.


$sql = "SELECT COUNT(*) FROM table";
$result = mysql_query($sql,$connection) or die("Couldn't execute $sql query. <br> mysql error: ".mysql_error()); ?>


$result = mysql_query("SELECT COUNT(*) FROM table");

allahverdi
08-04-2008, 06:10 AM
Actually, error handling is not necessary. But it is adviseable. Cause, if you have any error it will output the error. But if you won't put error handling, your script won't work, and no errors will be outputted, just blank page. And you won't find where the error is quickly.

kuau
08-04-2008, 06:12 AM
So the error handling is solely to help the programmer rather than to help the program? It doesn't do anything to improve the users' experience? If that is the case, then once your code is debugged and working fine, can you remove all that extra verbiage of the error handling? Thanks.

allahverdi
08-04-2008, 06:20 AM
I won't advice it. You can have error when you finished work too. It always helps i think.

Actually, if you want, you can remove it. But... It is your choose...

kuau
08-13-2008, 02:14 AM
Dear Allahverdi:

Now I see what you mean. I forgot how awful it is to get that blank page with no hints about what happened. All it took was one to remind me. I've put the error handling back in. Thanks for sharing your experience. :) e