views:

169

answers:

5

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

A: 

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

Douwe Maan
A: 

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());
vas3k
+5  A: 

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

Pascal MARTIN
A: 

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

Vinko Vrsalovic
+1  A: 

I would write a function like:

function PrintDebug($DebugData)
{ 
  if($DebugMode == 1)
  { 
      return nl2br($DebugData) . "<br />" . mysql_error();
  }
  else
  {
      return "Ops stuff got fucked 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.

Dr.Optix