Should you always end mysql queries with “or die?”

前端 未结 6 1905
野的像风
野的像风 2020-12-19 10:09

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 t

6条回答
  •  既然无缘
    2020-12-19 10:59

    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.

提交回复
热议问题