Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*************************************************
- # Database Class
- # Used to connect to the MySQL Database
- *************************************************/
- class ErrorMsg extends Exception {
- // Exception
- }
- class Database {
- // Variables
- private $host;
- private $user;
- private $pass;
- private $name;
- // Run the inputted information into the construct
- function __construct( $host, $user, $pass, $name ) {
- try {
- // Set the Variables
- $this->host = $host;
- $this->user = $user;
- $this->pass = $pass;
- $this->name = $name;
- // MySQL Connection
- $connect = @mysql_connect( $this->host, $this->user, $this->pass );
- $select = @mysql_select_db( $this->name );
- // If the connection failed then show message
- if( !$connect ) {
- throw new ErrorMsg( 'Error connecting to host.' );
- }
- // Else so it didn't fail then check the database selection
- elseif( !$select ) {
- throw new ErrorMsg( 'Error selecting database.' );
- }
- }
- // Catch any error messages and display them
- catch( ErrorMsg as $e ) {
- echo '<strong>Error</strong>';
- echo '<br />';
- echo $e->getMessage();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement