tags:

views:

57

answers:

2

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 qery and error message to a user with a session $_SESSION['auto_id'] == 1 and regualr users will just see a nice message saying there is an error

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 [email protected]<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() . "
        }
        ");
?>
+1  A: 

Try doing this after the mysql_query instead:

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.

Chacha102
+2  A: 

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:

<?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;
}
Tom Haigh
great thanks, sometimes its the simple things that slip the mind
jasondavis