2
$postQuery = $conn->query("SELECT * from table_name order by column desc")or die(mysql_error());

When there is error in query it ends the control and shows the error message in same page..

i want to redirect to an error page when error occurs and display the error in that page.

here is my Code :

$conn = new mysqli($host, $user, $password, $database);
    if ($conn->connect_error) {
        header("Location: ".SITEURL."/error.php?message=" . url_encode($conn->connect_error));
    } 

Any Suggestion.

4
  • Is this related to PHP Redirect Commented Oct 4, 2016 at 6:53
  • Yes, its php $conn->query() function. Commented Oct 4, 2016 at 6:54
  • just use an if/else Commented Oct 4, 2016 at 7:04
  • Yes @Ghost. but how can i send error information to another page.? Commented Oct 4, 2016 at 11:22

3 Answers 3

2

This is a very general error handler, suitable for any type of errors

Add this in the page where error is being generated

 //error handler function
 function customError($errno, $errstr) {
       $errorpage= "Location:error.php?&err=".$eerrno." ". $errstr;
 }

 //set error handler
 set_error_handler("customError");

Now make the page to display your error,

error.php

<?php
$err=$_GET['err'];
echo $err;
?>
Sign up to request clarification or add additional context in comments.

Comments

1

try {
    $postQuery = $conn->query("SELECT * from table_name order by column desc")or die(mysql_error());
} catch (MySQLException $e) {
    header("Location: http://example.com/");
}

If you get another error than MySQLException, then just replace that exception.

Comments

0

When your mysql query fails, it returns a False, and the connection holds the error data, which you can reference as such:

if (!$conn->query("SELECT * from table_name order by column desc")) {
  header("Location: http://www.yoursite.com/error.php?errormessage=" . url_encode($conn->error));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.