Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- * @DATABASE
- * ~~~~~~~~~~~~
- * @FILE DESCRIPTION: Handles all database related processes
- * @LAST MODIFIED: April 6, 2012
- */
- class database
- {
- private $dbc;
- private $row_count;
- function __construct($db_host, $db_name, $db_user, $db_password)
- {
- try
- {
- $this->dbc = new PDO("mysql:host=$db_host;dbname=$db_name", $db_user, $db_password);
- }
- catch(PDOException $e)
- {
- echo '<b>An error occured while trying to create a database connection: </b>'. $e->getMessage();
- }
- }
- /*
- * @METHOD setPDOAttribute
- * @DESC Sets a PDO attribute for our dbc object
- */
- public function setAttribute($first_param, $second_param)
- {
- try
- {
- $this->dbc->setAttribute($first_param, $second_param);
- }
- catch(PDOException $e)
- {
- echo 'PDOException Error: '. $e->getMessage();
- }
- }
- /*
- * @METHOD processInsertQuery
- * @DESC use for all queries BESIDES a select query
- */
- public function processQuery($query, array $binds, $fetch)
- {
- $query_handle = $this->dbc->prepare($query);
- if(!$query_handle->execute($binds))
- {
- $error = $query_handle->errorInfo();
- echo $error[2];
- }
- //incase we ever want to get the number of rows affected
- //we set our row_count variable to the number of rows
- //affected
- $this->row_count = $query_handle->rowCount();
- if($fetch == true)
- {
- return $query_handle->fetchAll();
- }
- }
- public function getRowCount()
- {
- return $this->row_count;
- }
- public function close_connection()
- {
- if($this->dbc != null)
- {
- $this->dbc = null;
- }
- }
- /*
- * @METHOD getDBCObject
- * @DESC Gives the $dbc object/variable to its child classes
- */
- public function getDBStatus()
- {
- if($this->dbc == null)
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- }
- ?>
Add Comment
Please, Sign In to add comment