PHP:die/exit 中的多个命令
当它出现错误时,我希望他做两件事。
- 回声 nl2br($qVraagOp);
- mysql_error();
所以我想:
$rVraagOp = mysql_query( $qVraagOp ) or die( echo nl2br($qVraagOp); mysql_error(); );
我可以编写一个函数来完成这两件事,然后调用它,但这有点多余。 还有其他办法吗?
马蒂
when it gives a error I want him to do 2 things.
- echo nl2br($qVraagOp);
- mysql_error();
so i thougth:
$rVraagOp = mysql_query( $qVraagOp ) or die( echo nl2br($qVraagOp); mysql_error(); );
I can write a function that does these two things and then call that but that a bit redundant.
is there a other way?
Matthy
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
仅仅因为技术错误消息而死并没有多大用处,至少对于您的最终用户来说是这样;您应该在设计网站时考虑到它们。
对任何人来说可能更有用的解决方案是:
不过,如果您确实需要这一点,您可以:
一个不错的解决方案可能是使用异常(半伪代码):
当然,这是考虑到您已经在某处定义了
MySQLException
。Exception 的一个好处是,所有处理错误的代码都位于一个位置:代码中间没有任何
die
just dying with a technical error message is not realy useful, at least for your end-users ; and you should design your site having them in mind.
A solution that's probably more useful to anyone would be to :
Still, if you really need this, you could :
A nice solution might be to use exceptions (semi-pseudo-code) :
Of course, this is considering you've defined the
MySQLException
somewhere.One nice thing with Exception is that all your code that deals with errors is at one place : there is no
die
everywhere in the middle of your code我会编写一个像这样的函数:
我会像你的工作一样使用它,
将调试状态保存在数据库中(认为维护模式是否处于活动状态)并且记录到文件也会有所帮助。另外我想指出我没有测试过。
I would write a function like:
And I would use it like
It's your job to save the debug status in a DB(think is maintenance mode active or not) and logging to a file would be helpful too. Also I want to point out that I didn't tested it.
那么您想同时显示
nl2br($qVraagOp)
和mysql_error()
吗?那将是(使用
PHP_EOL
常量在nl2br($qVraagOp)
和mysql_error()
之间放置换行符)So you want to show both
nl2br($qVraagOp)
andmysql_error()
? That would be(Using the
PHP_EOL
constant to place a newline betweennl2br($qVraagOp)
and themysql_error()
)我不应该在 PHP 上尝试这个,但它应该可以工作。抱歉,如果没有的话:)
I'm shouldnt try this on PHP, but it should work. Sorry, if not :)
您可以编写
die() 接收一个字符串(或一个 int)作为参数。
确保您的死亡消息仅存储在日志中,并且不会输出给最终用户,因为它通常是无用的,甚至是危险的(向潜在攻击者泄露有关您的配置的信息。)
You can write
die() receives a string (or an int) as an argument.
Be sure your dying message is only stored in the log and it doesn't output to the final user, as it's usually useless and even dangerous (reveals information about your configuration to potential attackers.)