您是否应该始终以“或死"结束mysql查询? [英] Should you always end mysql queries with "or die?"

查看:49
本文介绍了您是否应该始终以“或死"结束mysql查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

某些教程中的示例查询总是以

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?

推荐答案

否.

避免,不惜一切代价!

  1. 向最终用户展示这条消息真可怕
  2. mysql_error可能会公开您不想获得的信息
  3. 没有办法处理错误,即还原.
  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).

第一个将钱从X转移到Y,然后成功. 第二个必须从X 失败中减去Y.

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

您无法还原事务,并且不会记录该错误.有效地使用户Y感到高兴,而X使用户迷惑了钱去了哪里...

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...

对查询使用明智的错误处理-或者创建一个可以为您处理此类的类,或者使用ORM.

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

这篇关于您是否应该始终以“或死"结束mysql查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆