Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
494 views
in Technique[技术] by (71.8m points)

php - Should you always end mysql queries with "or die?"

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...