JayRogers

General purpose PHP/MySQL handler class utilizing PDO

Jul 20th, 2012
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.03 KB | None | 0 0
  1. /*
  2.     Description:
  3.         -> Base general purpose PDO MySQL database connection instantiation class.
  4.         -> contains set of common generic database interaction methods.
  5.         -> helper modules can be created for simplifying use of its generic methods,
  6.            and "plugged in" using the load_helper_module() method.
  7. */
  8. class db_connection
  9. {
  10.     /* property: connection object instance */
  11.     private $connection;
  12.  
  13.     /* properties: connection variables */
  14.     private $dbhost; // database host.
  15.     private $dbname; // database name.
  16.     private $dbuser; // database username.
  17.     private $dbpass; // database password.
  18.  
  19.     /* property: HM -> stores array of helper module objects */
  20.     public $HM;
  21.  
  22.     public function __construct()
  23.     {
  24.         /* set connection properties */
  25.         $this->dbhost = DB_HOST;
  26.         $this->dbname = DB_NAME;
  27.         $this->dbuser = DB_USER;
  28.         $this->dbpass = DB_PASS;
  29.  
  30.         /* instantiate connection */
  31.         try
  32.         {
  33.             $this->connection = new PDO(
  34.                 'mysql:host=' . $this->dbhost . ';dbname=' . $this->dbname,
  35.                 $this->dbuser,
  36.                 $this->dbpass
  37.             );
  38.             $this->connection->setAttribute(PDO::ATTR_PERSISTENT, true);
  39.             $this->connection->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
  40.             $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  41.         }
  42.         catch(PDOException $e)
  43.         {
  44.             jerk_debug('Error In: ' . __METHOD__ , $e->getMessage());
  45.             exit;
  46.         }
  47.  
  48.         /* prep $this->HM for storing helper modules */
  49.         $this->HM = array();
  50.     }
  51.  
  52.     public function __destruct()
  53.     {
  54.         $this->HM = null;
  55.         $this->connection = null;
  56.     }
  57.  
  58.     // method: register_helper()
  59.     //      -> creates a new helper module object with a preferred specified name.
  60.     //      -> $name is the desired name of the helper, $helper is the name of helper class.
  61.     //      -> helper modules contain "wrapper" methods to ease working with the generic
  62.     //         methods defined here (db_connection)
  63.     public function register_helper($name, $helper)
  64.     {
  65.         if(!isset($this->HM[$name]))
  66.         {
  67.             $this->HM[$name] = new $helper($this);
  68.         }
  69.     }
  70.  
  71.     /**********************************/
  72.     /*  TABLE CREATION and REPORTING  */
  73.     /**********************************/
  74.  
  75.     public function construct_table($table, $fields, $primary)
  76.     {
  77.         $sql = 'CREATE TABLE IF NOT EXISTS `' . $table . '` ( ';
  78.  
  79.         for($i = 0; $i < count($fields); $i++)
  80.         {
  81.             $sql .= '`' . $fields[$i][0] . '` ' . $fields[$i][1] . ', ';
  82.         }
  83.  
  84.         $sql .= 'PRIMARY KEY (`' . $primary . '`) ';
  85.  
  86.         $sql .= ' )';
  87.         $sql .= 'ENGINE=InnoDB CHARACTER SET=utf8';
  88.         try
  89.         {
  90.             $this->connection->exec($sql);
  91.         }
  92.         catch(PDOException $e)
  93.         {
  94.             jerk_debug('Error In: ' . __METHOD__ , $e->getMessage());
  95.         }
  96.     }
  97.  
  98.     public function report_table($table)
  99.     {
  100.         $tables_array = array();
  101.  
  102.         try
  103.         {   // get table names and push to $tables_array
  104.             $tablequery = $this->connection->query("
  105.                 SELECT
  106.                     TABLE_NAME
  107.                 FROM
  108.                     information_schema.TABLES
  109.                 WHERE
  110.                     TABLE_TYPE = 'BASE TABLE'
  111.                 AND
  112.                     TABLE_SCHEMA = '$this->dbname'
  113.             ");
  114.             while($fetch = $tablequery->fetch())
  115.             {
  116.                 array_push($tables_array, $fetch['TABLE_NAME']);
  117.             }
  118.         }
  119.         catch(PDOException $e)
  120.         {
  121.             jerk_debug('Error In: ' . __METHOD__ , $e->getMessage());
  122.         }
  123.  
  124.         if(in_array($table, $tables_array))
  125.         {
  126.             return true;
  127.         }
  128.         return false;
  129.     }
  130.  
  131.     public function drop_table($table)
  132.     {
  133.         if($this->report_table($table)){
  134.             try
  135.             {
  136.                 $this->connection->exec("DROP TABLE `$table`");
  137.             }
  138.             catch(PDOException $e)
  139.             {
  140.                 jerk_debug('Error In: ' . __METHOD__ , $e->getMessage());
  141.             }
  142.         }
  143.     }
  144.  
  145.     /********************/
  146.     /*  SELECT METHODS  */
  147.     /********************/
  148.  
  149.     // method: get_row_count -> returns the number of rows based on primary key entries.
  150.     public function fetch_row_count($table, $key)
  151.     {
  152.         if($this->report_table($table)){
  153.             try{
  154.                 $statement = $this->connection->query("
  155.                     SELECT COUNT(`$key`)
  156.                     AS `num`
  157.                     FROM `$table`
  158.                 ");
  159.                 $fetch = $statement->fetch();
  160.                 return $fetch['num'];
  161.             }
  162.             catch(PDOException $e){
  163.                 jerk_debug('Error In: ' . __METHOD__ , $e->getMessage());
  164.                 return null;
  165.             }
  166.         }
  167.         return null;
  168.     }
  169.  
  170.     public function check_duplicate($table, $collumn, $item)
  171.     {
  172.         try
  173.         {
  174.             $statement = $this->connection->prepare("
  175.                 SELECT COUNT(`$collumn`)
  176.                 AS `dup`
  177.                 FROM `$table`
  178.                 WHERE `$collumn` = :item
  179.             ");
  180.             $statement->bindParam(':item', $item);
  181.             $statement->execute();
  182.             $fetch = $statement->fetch();
  183.  
  184.             if($fetch['dup'] > 0)
  185.             { // if we have a find..
  186.                 return true;
  187.             }
  188.             return false;
  189.         }
  190.         catch(PDOException $e)
  191.         {
  192.             jerk_debug('Error In: ' . __METHOD__ , $e->getMessage());
  193.             return null;
  194.         }
  195.     }
  196.  
  197.     // public method: fetch_items() -> [ table name | items to get | [ from where | equals this ] ]
  198.     public function fetch_items($table, $items, $where, $values)
  199.     {
  200.         $sql = 'SELECT ';
  201.         // where are we selecting from?
  202.         if(is_array($items))
  203.         {   // are we dealing with an array?
  204.             for($i = 0; $i < count($items); $i++)
  205.             {
  206.                 if($i !== (count($items) - 1))
  207.                 {
  208.                     $sql .= '`' . $items[$i] . '`, ';
  209.                 }
  210.                 else
  211.                 {
  212.                     $sql .= '`' . $items[$i] . '` ';
  213.                 }
  214.             }
  215.         }
  216.         else
  217.         {   // if not an array
  218.             $sql .= '`' . $items . '` ';
  219.         }
  220.         // from which table?
  221.         $sql .= 'FROM `' . $table . '` ';
  222.         // where?
  223.         if(is_array($where))
  224.         {
  225.             for($i = 0; $i < count($where); $i++)
  226.             {
  227.                 if($i == 0)
  228.                 {
  229.                     $sql .= 'WHERE `' . $where[$i] . '` = :item' . $i . ' ';
  230.                 }
  231.                 else
  232.                 {
  233.                     $sql .= 'AND `' . $where[$i] . '` = :item' . $i . ' ';
  234.                 }
  235.             }
  236.         }
  237.         else
  238.         {
  239.             $sql .= 'WHERE `' . $where . '` = :item ';
  240.         }
  241.  
  242.         try
  243.         {
  244.             $statement = $this->connection->prepare($sql);
  245.             if(is_array($where))
  246.             {
  247.                 for($i = 0; $i < count($where); $i++)
  248.                 {
  249.                     $statement->bindParam(':item' . $i, $values[$i]);
  250.                 }
  251.             }
  252.             else
  253.             {
  254.                 $statement->bindParam(':item', $values);
  255.             }
  256.             $statement->execute();
  257.             $fetch = $statement->fetch();
  258.             if(is_array($fetch))
  259.             {
  260.                 return $fetch;
  261.             }
  262.             return null;
  263.         }
  264.         catch(PDOException $e)
  265.         {
  266.             jerk_debug('Error In: ' . __METHOD__ , $e->getMessage());
  267.         }
  268.     }
  269.  
  270.     /********************/
  271.     /*  UPDATE METHODS  */
  272.     /********************/
  273.  
  274.     public function update_timestamp($table, $update, $where, $values)
  275.     {
  276.         $sql = 'UPDATE `' . $table . '` ';
  277.         $sql .= 'SET `' . $update . '` = NOW() ';
  278.         $sql .= 'WHERE `' . $where . '` = :item ';
  279.         try
  280.         {
  281.             $statement = $this->connection->prepare($sql);
  282.             $statement->bindParam(':item', $values);
  283.             $statement->execute();
  284.         }
  285.         catch(PDOException $e)
  286.         {
  287.             jerk_debug('Error In: ' . __METHOD__ , $e->getMessage());
  288.         }
  289.     }
  290.  
  291.     public function update_fields($table, $updatecols, $updatevals, $wherecols, $wherevals)
  292.     {
  293.         $sql = 'UPDATE `' . $table . '` SET ';
  294.  
  295.         if(is_array($updatecols))
  296.         {
  297.             for($i = 0; $i < count($updatecols); $i++)
  298.             {
  299.                 if($i !== (count($updatecols) - 1))
  300.                 {
  301.                     $sql .= '`' . $updatecols[$i] . '` = :update' . $i . ', ';
  302.                 }
  303.                 else
  304.                 {
  305.                     $sql .= '`' . $updatecols[$i] . '` = :update' . $i . ' ';
  306.                 }
  307.             }
  308.         }
  309.         else
  310.         {
  311.             $sql .= '`' . $updatecols . '` = :update ';
  312.         }
  313.  
  314.         if(is_array($wherecols))
  315.         {
  316.             for($i = 0; $i < count($wherecols); $i++)
  317.             {
  318.                 if($i == 0)
  319.                 {
  320.                     $sql .= 'WHERE `' . $wherecols[$i] . '` = :where' . $i . ' ';
  321.                 }
  322.                 else
  323.                 {
  324.                     $sql .= 'AND `' . $wherecols[$i] . '` = :where' . $i . ' ';
  325.                 }
  326.             }
  327.         }
  328.         else
  329.         {
  330.             $sql .= 'WHERE `' . $wherecols . '` = :where ';
  331.         }
  332.  
  333.         try
  334.         {
  335.             $statement = $this->connection->prepare($sql);
  336.  
  337.             if(is_array($updatecols))
  338.             {
  339.                 for($i = 0; $i < count($updatecols); $i++)
  340.                 {
  341.                     $statement->bindParam(':update' . $i, $updatevals[$i]);
  342.                 }
  343.             }
  344.             else
  345.             {
  346.                 $statement->bindParam(':update', $updatevals);
  347.             }
  348.  
  349.             if(is_array($wherecols))
  350.             {
  351.                 for($i = 0; $i < count($wherecols); $i++)
  352.                 {
  353.                     $statement->bindParam(':where' . $i, $wherevals[$i]);
  354.                 }
  355.             }
  356.             else
  357.             {
  358.                 $statement->bindParam(':where', $wherevals);
  359.             }
  360.  
  361.             $statement->execute();
  362.         }
  363.         catch(PDOException $e)
  364.         {
  365.             jerk_debug('Error In: ' . __METHOD__ , $e->getMessage());
  366.         }
  367.     }
  368.  
  369.     /********************/
  370.     /*  INSERT METHODS  */
  371.     /********************/
  372.  
  373.     public function insert_into_table($table, $collumns, $values)
  374.     {
  375.         $sql = 'INSERT INTO `' . $table . '` (';
  376.         for($i = 0; $i < count($collumns); $i++)
  377.         {
  378.             if($i !== (count($collumns) - 1))
  379.             {
  380.                 $sql .= '`' . $collumns[$i] . '`, ';
  381.             }
  382.             else
  383.             {
  384.                 $sql .= '`' . $collumns[$i] . '`';
  385.             }
  386.         }
  387.         $sql .= ') VALUES (';
  388.         for($i = 0; $i < count($values); $i++)
  389.         {
  390.             if($i !== (count($values) - 1))
  391.             {
  392.                 $sql .= ':item' . $i . ', ';
  393.             }
  394.             else
  395.             {
  396.                 $sql .= ':item' . $i;
  397.             }
  398.         }
  399.         $sql .= ')';
  400.  
  401.         try
  402.         {
  403.             $statement = $this->connection->prepare($sql);
  404.             for($i = 0; $i < count($values); $i++)
  405.             {
  406.                 $statement->bindParam(':item' . $i, $values[$i]);
  407.             }
  408.             $statement->execute();
  409.         }
  410.         catch(PDOException $e)
  411.         {
  412.             jerk_debug('Error In: ' . __METHOD__ , $e->getMessage());
  413.             return false;
  414.         }
  415.         return true;
  416.     }
  417. }
Advertisement
Add Comment
Please, Sign In to add comment