PHP header location

Status
Not open for further replies.

Sean

‫‫‫‫‫‫  ‫  Don't Worry, Be Happy
Dec 12, 2011
1,121
405
I just made a config page, and It connects to the database, but I want it to redirect to an error page that I made when the connection fails instead just displaying an error. Here is my code:

E8rlW.png
 

Xenous

o shi
Nov 15, 2011
383
101
PHP:
<?php
 
    $connection = mysql_connect($host, $user, $pass);
    if(!$connection)
    {
        header("Location: http://example.com/pageToBeRedirectedTo");
    }
   
    $db = mysql_select_db($data, $connection) or die("Error");
    /* You could also have
    *
    *if(!$db)
    *{
    *    header("Location: http://example.com/pageToBeRedirectedTo");
    *}
    *
    */
 
?>
 

Sean

‫‫‫‫‫‫  ‫  Don't Worry, Be Happy
Dec 12, 2011
1,121
405
PHP:
<?php
 
    $connection = mysql_connect($host, $user, $pass);
    if(!$connection)
    {
        header("Location: http://example.com/pageToBeRedirectedTo");
    }
 
    $db = mysql_select_db($data, $connection) or die("Error");
    /* You could also have
    *
    *if(!$db)
    *{
    *    header("Location: http://example.com/pageToBeRedirectedTo");
    *}
    *
    */
 
?>


Will that not redirect it if the connection is successful ?
 

Twix

Member
Mar 31, 2012
81
8
No because if you look closely he's used an exclamation mark within the if statement, this means if "$connection" is false, it'll run the code within the statement.
 

Sean

‫‫‫‫‫‫  ‫  Don't Worry, Be Happy
Dec 12, 2011
1,121
405
Just realised that there is no need for this, It just makes the files look messy. I have just styled the error messages :)

Thread can be closed now...
 
Status
Not open for further replies.

Users who are viewing this thread

Top