views:

55

answers:

2

For everyone that was thinking of the error_reporting() function, then it isn't, what I need is whenever a MySQL query has been done, and the statement has like

if($result)
{
    echo "Yes, it was fine... bla bla";
}
else
{
    echo "Obviously, the echo'ing will show in a white page with the text ONLY...";
}

Whenever statements have been true or false, I want the error to be appeared when redirected with the header() function and echo the error reporting in a div somewhere on the page.

Basically something like this:

$error = '';

This part appears inside the div tags

<div><?php echo $error; ?></div>

So the error part will be echoed when redirected with the header()

if($result)
{
    $error = "Yes, it was fine... bla bla";
    header("Location: url");
}
else
{
    $error = "Something wrong happened...";
    header("Location: url");
}   

But that just doesn't work :(

+1  A: 

You can use $_SESSION superglobal.

Sample code:

/* in mysql result checking */
$_SESSION["error"] = "Something wrong happened...";

/* in div tags */
if(!empty($_SESSION["error"])) {
    echo $_SESSION["error"];
    $_SESSION["error"] = "";
}

I also think that you need to call session_start() in the start of your script, if that wasn't done before.

Best regards,
T.

Thiago Silveira
+1  A: 

That won't work because error is not declared in url. You're just discarding (unsetting) the error variable when you send the location header.

You should make your own error codes, for example:

0001 = ok
0002 = DB_error
... 

and send them trough GET variables, catch'em in url and parse them.

header('Location: url?error='.urlencode($error));
Ben
Please wrap $error in urlencode() before gluing it to an url address. Thank you. ;-)
Kamil Szot