如何在屏幕上显示此mysql错误

时间:2021-04-04 22:21:20

Current function to run mysql query in PHP and displays error on screen when there is one. I am trying to make it only show the query and error message to a user with a session $_SESSION['auto_id'] == 1 and regular users will just see a nice message saying there is an error

当前函数在PHP中运行mysql查询并在有屏幕时显示错误。我试图让它只向具有会话$ _SESSION ['auto_id'] == 1的用户显示查询和错误消息,而普通用户只会看到一条消息说有错误

In the second example I attempted to do it but I am not sure how to parse the code correctly inside of or die(" ")

在第二个例子中我试图这样做,但我不知道如何在内部正确解析代码或死(“”)

Works:

<?PHP    
    function executeQuery($sql) {
        $result = mysql_query($sql) or die("<span style='FONT-SIZE:11px; FONT-COLOR: #000000; font-family=tahoma;'><center>An Internal Error has Occured. Please report following error to error@friendproject.com<br><br>" . $sql . "<br><br>" . mysql_error() . "'</center></FONT>");
        return $result;
    }
?>

Doesn't Work:

<?PHP
    function executeQuery($sql) {
        $result = mysql_query($sql) or die("
        <span style='FONT-SIZE:11px; FONT-COLOR: #000000; font-family=tahoma;'><center>An Internal Error has Occured. The error has been recorded<br>'</center></FONT>
        if($_SESSION['auto_id'] == 1){
            echo = '<br>' . $sql . '<br><br>' . mysql_error() . "
        }
        ");
?>

2 个解决方案

#1


A string passed to die() will just be sent to the browser and any PHP code within it will just be treated as a literal string and not interpreted. You probably want something more like this:

传递给die()的字符串将被发送到浏览器,其中的任何PHP代码都将被视为文字字符串而不被解释。你可能想要更像这样的东西:

<?PHP
function executeQuery($sql) {
    $result = mysql_query($sql);
    if (!$result) {
         $error = "<span style='FONT-SIZE:11px; FONT-COLOR: #000000; font-family=tahoma;'><center>An Internal Error has Occured. The error has been recorded<br>'</center></FONT>";

        if($_SESSION['auto_id'] == 1){
            //append mysql error to string we are about to output
            $error .= '<br>' . $sql . '<br><br>' . mysql_error() ;
        }
       die($error);
    }
    return $result;
}

#2


Try doing this after the mysql_query instead:

尝试在mysql_query之后执行此操作:

if(mysql_error())
{
    // Do Stuff

  if($_SESSION['auto_id'] == 1)
  {
    // Do More Stuff
  }
die();
}

This will allow you to achieve the same affect, and format your error as well.

这将允许您实现相同的效果,并格式化您的错误。

As to why your statement doesn't work: Ifs are not executed while inside quotes.

至于为什么你的语句不起作用:ifs不在引号内执行。

#1


A string passed to die() will just be sent to the browser and any PHP code within it will just be treated as a literal string and not interpreted. You probably want something more like this:

传递给die()的字符串将被发送到浏览器,其中的任何PHP代码都将被视为文字字符串而不被解释。你可能想要更像这样的东西:

<?PHP
function executeQuery($sql) {
    $result = mysql_query($sql);
    if (!$result) {
         $error = "<span style='FONT-SIZE:11px; FONT-COLOR: #000000; font-family=tahoma;'><center>An Internal Error has Occured. The error has been recorded<br>'</center></FONT>";

        if($_SESSION['auto_id'] == 1){
            //append mysql error to string we are about to output
            $error .= '<br>' . $sql . '<br><br>' . mysql_error() ;
        }
       die($error);
    }
    return $result;
}

#2


Try doing this after the mysql_query instead:

尝试在mysql_query之后执行此操作:

if(mysql_error())
{
    // Do Stuff

  if($_SESSION['auto_id'] == 1)
  {
    // Do More Stuff
  }
die();
}

This will allow you to achieve the same affect, and format your error as well.

这将允许您实现相同的效果,并格式化您的错误。

As to why your statement doesn't work: Ifs are not executed while inside quotes.

至于为什么你的语句不起作用:ifs不在引号内执行。