tags:

views:

162

answers:

6

Example queries in some tutorials ALWAYS end with:

or die(mysql_error());  

I can see why you would would sometimes want to do this, but they even use this for queries that really shouldn't cause a problem. Is it a good practice to always use this, or are they just doing it to help you debug as you learn?

A: 

The word "shouldn't" is bad here. Queries that shouldn't have a problem can still be affected by external factors, e.g. connection to the database going down.

Ignacio Vazquez-Abrams
A: 

but they even use this for queries that really shouldn't cause a problem

Even the simplest SELECT * FROM FOO can cause a problem if the network between the web server and the database server falls over. It's good practice because when dealing with external systems such as databases, you really can't ever assume that something is 'safe'.

In production code, you may not wish to use die() to handle errors - you could perhaps run some custom code to deal with it. At any rate, you should definitely handle the error in one way or another, and die() is a good way to start.

Orion Edwards
+1  A: 

It depends on what you mean by "shouldn't be a problem".

If you mean "well it won't fail", what happens if the database server goes offline?

Only if you mean "It doesn't matter whether it fails or not, the script can keep running without a problem" that you should consider not having the or die there.

Anon.
A: 

You don't necessarily need

or die()

But you should have some kind of error handling system. If you don't have one in place yet, I would add it to my queries, even the simple ones.

EDIT: To clarify, by "it" I mean the or die() statement.

Tyler Smith
+11  A: 

NO.

Avoid that at all cost!

  1. It's a horrible message to show an end user
  2. mysql_error may expose information you don't want to be given
  3. There is no way to handle the error, i.e revert.

Imagine a database of transactions - your customer sends money, so you have to modify two tables (two queries).

First one transfers money from X to Y and succeeds. The second one has to subtract Y from X fails.

You have no way to revert the transaction and the error is not logged. Effectively making user Y happy and X left confuse where the money went...

Use a sensible error handling for queries - either make a class that will handle that for you or use ORM.

LiraNuna
completely agree - the use of 'or die()' should be stricken from manuals and books, and proper error-handling (even if barebones) should be shown.
HorusKol
Ok, please please please educate the rookie OP and this rookie commenter on where to turn for rock-solid PHP error-handling that's framework independent?
Andrew Heath
@Andrew, Using a framework is a good start - once you're comfortable with the framework, you can peek into their sources and see how they handle the errors.
LiraNuna
+1  A: 

OR meaning like that, can only be used with DIE ? I mean, could I do something like this;

mysql_query($sql) or My_Error_Handler(mysql_error());
Arthur Corenzan