Avatar of slightlyoff
slightlyoff
 asked on

PHP Redirect on Database Connection Error

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:

<?php
session_start();
$error_text = $_SESSION['dberror'];
include 'inc/dbconnect.php';
include 'inc/session_handle.php';
?>

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:
<?php
$conn = mysql_connect("localjost","root");  //misspelled to trigger error
if (!$conn)
  {
  $_SESSION['dberror'] = mysql_error();
 
        /* Redirect browser */
      header("Location: dberror.php");
      exit;
  }
 
$dbname = 'myown_db';
mysql_select_db($dbname);
?>

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.

Thanks!!!
PHPMySQL Server

Avatar of undefined
Last Comment
Ray Paseur

8/22/2022 - Mon
SOLUTION
Ess Kay

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
Ray Paseur

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
slightlyoff

ASKER
Thank you both for your help!
There are a lot of things I like about PHP - and although I dabbled in ASP.NET, it seems PHP does what I need.  If I can get around the strange things :)

Thanks for the link - I'll read up on that!
Ray Paseur

Thanks for the points.  You'll love the SitePoint book.  Very readable with great examples. ~Ray
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes