pdo connection status turned into a variable to echo elsewhere.

Associate
Joined
11 Oct 2008
Posts
268
Hey guys,

I am currently using the following code to connect to my database and to display custom messages for if it has connected to the database or not.

I would quite like to display these messages in my footer.

I was wondering if its possible to turn the messages into a variable to echo them in the footer? Thanks for any tips.

PHP:
$hostname = 'localhost';
    $database = '';
    $username = '';
    $password = '';
      try {
    $dbh = new PDO("mysql:host=$hostname;dbname=$database", $username, $password);
      echo 'Connected to database'; }
      catch(PDOException $e) {
      echo 'error connecting to database.'; }
      error_reporting(0);
 
Associate
Joined
18 Sep 2003
Posts
903
PHP:
    $hostname = 'localhost';
    $database = '';
    $username = '';
    $password = '';
    $db_connection_status = '';
    try {
       $dbh = new PDO("mysql:host=$hostname;dbname=$database", $username, $password);
       $db_connection_status =  'Connected to database'; 
    }catch(PDOException $e) {
        $db_connection_status = 'error connecting to database.'; 
    }
    error_reporting(0);

Then in the footer where you want to display the message:

PHP:
<?=$db_connection_status?>
 
Back
Top Bottom