PHP:die/exit 中的多个命令

发布于 2024-08-22 17:11:24 字数 276 浏览 2 评论 0原文

当它出现错误时,我希望他做两件事。

  1. 回声 nl2br($qVraagOp);
  2. 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.

  1. echo nl2br($qVraagOp);
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

Smile简单爱 2024-08-29 17:11:24

仅仅因为技术错误消息而死并没有多大用处,至少对于您的最终用户来说是这样;您应该在设计网站时考虑到它们。

对任何人来说可能更有用的解决方案是:

  • 将技术错误消息记录到文件中,您有时会检查该文件在
  • 末尾显示一个漂亮的“哎呀,发生错误”页面 -用户。

不过,如果您确实需要这一点,您可以:

  • 连接这两个信息,只有一个字符串
  • 使用 if/else-block ;这可能会让你的代码更容易阅读。

一个不错的解决方案可能是使用异常(半伪代码)

当然,这是考虑到您已经在某处定义了MySQLException

try {
    // Do some stuff

    if (!mysql_query(...)) {
        throw new MySQLException(mysql_error());
    }

    // Some other stuff
} catch (MySQLException $e) {
    // Deal with the MySQLException kind of Exception
    // i.e. deal my SQL errors
    // => Log to file
    //    + display a nice error page
} catch (Exception $e) {
    // At least, if something in the try block
    // might raise an Exception that's not a 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 :

  • Log the technical error message to a file, that you'll sometimes check
  • Display a nice "oops, an error has occured" page to the end-user.

Still, if you really need this, you could :

  • Concatenate both informations, to only have one string
  • use an if/else-block ; which might make your code more easy to read.

A nice solution might be to use exceptions (semi-pseudo-code) :

Of course, this is considering you've defined the MySQLException somewhere.

try {
    // Do some stuff

    if (!mysql_query(...)) {
        throw new MySQLException(mysql_error());
    }

    // Some other stuff
} catch (MySQLException $e) {
    // Deal with the MySQLException kind of Exception
    // i.e. deal my SQL errors
    // => Log to file
    //    + display a nice error page
} catch (Exception $e) {
    // At least, if something in the try block
    // might raise an Exception that's not a MySQLException
}

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

小矜持 2024-08-29 17:11:24

我会编写一个像这样的函数:

function PrintDebug($DebugData)
{ 
  if($DebugMode == 1)
  { 
      return nl2br($DebugData) . "<br />" . mysql_error();
  }
  else
  {
      return "Ops stuff got messed up!!!";
  }
} 

我会像你的工作一样使用它,

$rVraagOp = mysql_query( $qVraagOp ) or die(PrintDebug($qVraagOp));

将调试状态保存在数据库中(认为维护模式是否处于活动状态)并且记录到文件也会有所帮助。另外我想指出我没有测试过。

I would write a function like:

function PrintDebug($DebugData)
{ 
  if($DebugMode == 1)
  { 
      return nl2br($DebugData) . "<br />" . mysql_error();
  }
  else
  {
      return "Ops stuff got messed up!!!";
  }
} 

And I would use it like

$rVraagOp = mysql_query( $qVraagOp ) or die(PrintDebug($qVraagOp));

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.

孤凫 2024-08-29 17:11:24

那么您想同时显示 nl2br($qVraagOp)mysql_error() 吗?那将是

or die(nl2br($qVraagop) . PHP_EOL . mysql_error())

(使用 PHP_EOL 常量在 nl2br($qVraagOp)mysql_error() 之间放置换行符)

So you want to show both nl2br($qVraagOp) and mysql_error()? That would be

or die(nl2br($qVraagop) . PHP_EOL . mysql_error())

(Using the PHP_EOL constant to place a newline between nl2br($qVraagOp) and the mysql_error())

别想她 2024-08-29 17:11:24

我不应该在 PHP 上尝试这个,但它应该可以工作。抱歉,如果没有的话:)

$rVraagOp = mysql_query( $qVraagOp ) or echo(nl2br($qVraagOp)) and die(mysql_error());

I'm shouldnt try this on PHP, but it should work. Sorry, if not :)

$rVraagOp = mysql_query( $qVraagOp ) or echo(nl2br($qVraagOp)) and die(mysql_error());
故乡的云 2024-08-29 17:11:24

您可以编写

$rVraagOp = mysql_query( $qVraagOp ) 
          or die( sprintf("%s%s%s", nl2br($qVraagop), PHP_EOL, mysql_error()) );

die() 接收一个字符串(或一个 int)作为参数。

确保您的死亡消息仅存储在日志中,并且不会输出给最终用户,因为它通常是无用的,甚至是危险的(向潜在攻击者泄露有关您的配置的信息。)

You can write

$rVraagOp = mysql_query( $qVraagOp ) 
          or die( sprintf("%s%s%s", nl2br($qVraagop), PHP_EOL, mysql_error()) );

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文