PHP Redirect on Database Connection Error
Posted on 2012-03-12
I'm new to PHP - after doing a lot of ASP and I'm stumbling on a few issues.
The first issue I have is redirecting to a new page during error handling.
I have the following code:
$error_text = $_SESSION['dberror'];
dbconnect.php establishes the connection to the database.
If there's an error, I'd like to redirect to a friendly error page, since the code can't continue.
my dbconnect.php page looks like this:
$conn = mysql_connect("localjost","root"); //misspelled to trigger error
$_SESSION['dberror'] = mysql_error();
/* Redirect browser */
$dbname = 'myown_db';
This is the result I get:
Warning: mysql_connect() [function.mysql-connect]: Unknown MySQL server host 'localjost' (1) in /Applications/XAMPP/xamppfiles/htdocs/mysite/inc/dbconnect.php on line 3
Warning: Cannot modify header information - headers already sent by (output started at /Applications/XAMPP/xamppfiles/htdocs/mysite/inc/dbconnect.php:3) in /Applications/XAMPP/xamppfiles/htdocs/mysite/inc/dbconnect.php on line 9
In ASP this was pretty easy to code. i could redirect people to however many pages I needed to based on what error was triggered. It doesn't appear that I can do that in PHP.
(or, at least it appears I need to do it in a much different way.)
Any help you can provide would be appreciated.