Guest User

extendPDO

a guest
Dec 23rd, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.42 KB | None | 0 0
  1. <?php
  2. // Connect to the database using PDO and MySQL
  3. class DbConnect extends PDO
  4. {
  5.     protected $dbPassword = '';
  6.     protected $dbUsrName  = '';
  7.     protected $opt = array(
  8.     PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
  9.     PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
  10.     );
  11.    
  12.      function __construct()
  13.     {
  14.         parent::__construct('mysql:host=localhost;dbname=DBNAME;', 'user', 'password', $this->opt);
  15.         echo "Connected to Database";
  16.     }
  17.     var $stmt;
  18.     var $result_set;
  19.     function execQuery($query, $values, $return)
  20.     {
  21.         // prepares passed in query, query must use "?" as placeholders for values
  22.         $this->stmt = parent::prepare($query);
  23.         // loops through $values array, check each value's type and binds it as such
  24.         foreach($values as $i => $value)
  25.         {
  26.             $j = $i + 1;
  27.             if (is_int($value))
  28.                 $this->stmt->bindValue($j, $value, PDO::PARAM_INT);
  29.             else if (is_bool($value))
  30.                 $this->stmt->bindValue($j, $value, PDO::PARAM_BOOL);
  31.             else if (is_string($value))
  32.                 $this->stmt->bindValue($j, $value, PDO::PARAM_STR);
  33.         }
  34.         if (!$this->stmt->execute())
  35.             return false;
  36.         else if ($return)
  37.             $this->result_set = $this->stmt->fetchAll();
  38.     }
  39.     function CloseConnection()
  40.     {
  41.         $this->DbConnection = null;
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment