Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

PHP Redirect On Database Connect Fail

Hi!

I have the following code in admin/config/dbcon.php which establishes a connection to a database called "costing".

Code:
<?php

  $host = "localhost";
  $username = "root";
  $password = "";
  $database = "costing";

  $con = mysqli_connect("$host","$username","$password","$database");

  if(!$con) {
    header("Location: ../errors/dberror.php");
    die();
  }

?>

The admin/config/dbcon.php is called as an 'include' in the admin/index.php file:

Code:
<?php
  include('config/dbcon.php');
?>

If the connection fails, what I want to happen is to be redirected to ../errors/dberror.php - but it does not. Instead of redirecting, it simply gives me a fatal error message on the page (tested by changing the database name in the above code to something else): Fatal error: Uncaught mysqli_sql_exception: Unknown database 'costingblahblah'...

Below is the file structure where you can see the 3 files mentioned in this thread:

Error.JPG

Any help would be greatly appreciated!

Thank you in advance.
 
Solution
You have to redirect to a fully qualified URL. For example:

PHP:
header("Location: https://www.example.com/error_page.php");
die();

Also, make sure that there are never any echo statements or content outputted to the browser anywhere in your code before this header(). You will get a PHP fatal error if you try to echo HTML to the web browser before all header() calls are done with.
You might try the "set_error_handler"
Or use the try - catch method.
Never done them myself because I only had one DB and loss of it IS a fatal error and things should be shut down (in my case anyway).
 
You have to redirect to a fully qualified URL. For example:

PHP:
header("Location: https://www.example.com/error_page.php");
die();

Also, make sure that there are never any echo statements or content outputted to the browser anywhere in your code before this header(). You will get a PHP fatal error if you try to echo HTML to the web browser before all header() calls are done with.
 
Solution
You have to redirect to a fully qualified URL. For example:

PHP:
header("Location: https://www.example.com/error_page.php");
die();

Also, make sure that there are never any echo statements or content outputted to the browser anywhere in your code before this header(). You will get a PHP fatal error if you try to echo HTML to the web browser before all header() calls are done with.
Thank you very much, and apologies for the late reply. It worked with a fully qualified URL as you suggested.
 

New Threads

Buy us a coffee!

Back
Top Bottom