Advertisement
Guest User

Untitled

a guest
Sep 29th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. <?php
  2.  
  3. /*************************************************
  4. # Database Class
  5. # Used to connect to the MySQL Database
  6. *************************************************/
  7.  
  8. class ErrorMsg extends Exception {
  9.  
  10. // Exception
  11.  
  12. }
  13.  
  14. class Database {
  15.  
  16. // Variables
  17. private $host;
  18. private $user;
  19. private $pass;
  20. private $name;
  21.  
  22. // Run the inputted information into the construct
  23. function __construct( $host, $user, $pass, $name ) {
  24.  
  25. try {
  26.  
  27. // Set the Variables
  28. $this->host = $host;
  29. $this->user = $user;
  30. $this->pass = $pass;
  31. $this->name = $name;
  32.  
  33. // MySQL Connection
  34. $connect = @mysql_connect( $this->host, $this->user, $this->pass );
  35. $select = @mysql_select_db( $this->name );
  36.  
  37. // If the connection failed then show message
  38. if( !$connect ) {
  39.  
  40. throw new ErrorMsg( 'Error connecting to host.' );
  41.  
  42. }
  43.  
  44. // Else so it didn't fail then check the database selection
  45. elseif( !$select ) {
  46.  
  47. throw new ErrorMsg( 'Error selecting database.' );
  48.  
  49. }
  50.  
  51. }
  52.  
  53. // Catch any error messages and display them
  54. catch( ErrorMsg as $e ) {
  55.  
  56. echo '<strong>Error</strong>';
  57. echo '<br />';
  58. echo $e->getMessage();
  59.  
  60. }
  61.  
  62. }
  63.  
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement