Advertisement
hombretao

Exceptions

Nov 29th, 2013
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.52 KB | None | 0 0
  1. // index.php
  2. <?
  3. error_reporting(0);
  4. require_once('db.php');
  5. $db = new Database( 'root', 'asdf', 'zen' );
  6. if( is_string( $db ) )
  7. {
  8.     echo $db;
  9. }
  10.  
  11. // db.php
  12. <?
  13. class Database
  14. {
  15.     function __construct( $user, $password, $database )
  16.     {
  17.         try
  18.         {
  19.             $dbObject = new mysqli( 'localhost', $user, $password, $database );
  20.             if( $dbObject->connect_error )
  21.             {
  22.                 throw new Exception( $dbObject->connect_error );
  23.             }
  24.         }
  25.         catch( Exception $e )
  26.         {
  27.             return 'Error: '.$e->getMessage();
  28.         }
  29.         return $dbObject;
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement