Advertisement
Guest User

Database Constructor PHP Tutorial

a guest
May 2nd, 2018
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.81 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Simple PHP Class That interacts with a database object
  4.  */
  5.  
  6. /**
  7.  * This is the database wrapper we will use to interact with the database object
  8.  * known as `PDO`.
  9.  */
  10. class Database
  11. {
  12.     /**
  13.      * This is the database variable that stores are PDO object.
  14.      * The PDO object is the main object that will communicate to the database server.
  15.      * I made the database object protected, as this allows/forces me to write clean,
  16.      * and safe code, by forcing all database logic to be executed from the Database
  17.      * class itself.
  18.      *
  19.      * The variable name is not important, it could be pdo, or instance.
  20.      */
  21.     protected $database;
  22.    
  23.     /**
  24.      * This is the database constructor that will initialize are connection
  25.      * to the database server.
  26.      *
  27.      * @param hostname. This is the hostname/ip address/domain the mysql server is located at.
  28.      * @param username. This is the username that is allowed to connect to the database server.
  29.      * @param password. This is the password for the username.
  30.      * @param database. This is the name of the database we wanna connect to and use.
  31.      */
  32.     public function __construct($hostname = '127.0.0.1', $username = 'guest', $password = '', $database = 'testdb')
  33.     {
  34.         /**
  35.          * The PDO driver expects a dns string that tells the driver what database
  36.          * server we are using, the database name, and the hostname itself.
  37.          *
  38.          * PHP Strings can append other strings with . seperating the strings.
  39.          */
  40.         $dns = 'mysql:dbname=' . $database . ';host=' . $hostname;
  41.  
  42.         /**
  43.          * Another way we can achievement this is with using a function called sprintf
  44.          * which is a string formatting buffer.
  45.          * The formated string will be 'mysql:dbname=%s;hostname=%s'.
  46.          * The '%s' is telling sprintf that the first and second variable will be a string.
  47.          * sprintf also allows integers, or decimals using '%d'.
  48.          */
  49.         $formatted_dns = sprintf('mysql:dbname=%s;host=%s', $database, $hostname);
  50.  
  51.         /**
  52.          * Unlike the `mysqli` driver PDO will try to initialize, and if it can't it will throw an
  53.          * exception, To catch the exception we have to use a try block in php.
  54.          */
  55.         try {
  56.             /**
  57.              * $this-> is an accessor for variables declared inside the class object/scope.
  58.              * $this-> can access variables or functions also known as methods inside an object.
  59.              *
  60.              * PDO expects 3 variables at least, usually. The first being the dns string.
  61.              * The second and third being the username and password.
  62.              */
  63.             $this->database = new PDO($formatted_dns, $username, $password);
  64.         } catch (PDOException $exception) {
  65.             /**
  66.              * If the database could not be connected to for any reason,
  67.              * like the server being down, or the PDO driver not being installed
  68.              * an exception will be thrown.
  69.              */
  70.             echo 'The database connection failed: ' . $exception->getMessage();
  71.         }
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement