Skip to content

Instantly share code, notes, and snippets.

@jonahlyn
Created September 1, 2011 16:59
Show Gist options
  • Select an option

  • Save jonahlyn/1186647 to your computer and use it in GitHub Desktop.

Select an option

Save jonahlyn/1186647 to your computer and use it in GitHub Desktop.

Revisions

  1. jonahlyn created this gist Sep 1, 2011.
    103 changes: 103 additions & 0 deletions DBConnection.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,103 @@
    <?php
    /*
    * Class DBConnection
    * Create a database connection using PDO
    * @author [email protected]
    *
    * Instructions for use:
    *
    * require_once('settings.config.php'); // Define db configuration arrays here
    * require_once('DBConnection.php'); // Include this file
    *
    * $database = new DBConnection($dbconfig); // Create new connection by passing in your configuration array
    *
    * $sqlSelect = "select * from ....."; // Select Statements:
    * $rows = $database->getQuery($sqlSelect); // Use this method to run select statements
    *
    * foreach($rows as $row){
    * echo $row["column"] . "<br/>";
    * }
    *
    * $sqlInsert = "insert into ...."; // Insert/Update/Delete Statements:
    * $count = $database->runQuery($sqlInsert); // Use this method to run inserts/updates/deletes
    * echo "number of records inserted: " . $count;
    *
    * $name = "jonahlyn"; // Prepared Statements:
    * $stmt = $database->dbc->prepare("insert into test (name) values (?)");
    * $stmt->execute(array($name));
    *
    */

    Class DBConnection {

    // Database Connection Configuration Parameters
    // array('driver' => 'mysql','host' => '','dbname' => '','username' => '','password' => '')
    protected $_config;

    // Database Connection
    public $dbc;

    /* function __construct
    * Opens the database connection
    * @param $config is an array of database connection parameters
    */
    public function __construct( array $config ) {
    $this->_config = $config;
    $this->getPDOConnection();
    }

    /* Function __destruct
    * Closes the database connection
    */
    public function __destruct() {
    $this->dbc = NULL;
    }

    /* Function getPDOConnection
    * Get a connection to the database using PDO.
    */
    private function getPDOConnection() {
    // Check if the connection is already established
    if ($this->dbc == NULL) {
    // Create the connection
    $dsn = "" .
    $this->_config['driver'] .
    ":host=" . $this->_config['host'] .
    ";dbname=" . $this->_config['dbname'];

    try {
    $this->dbc = new PDO( $dsn, $this->_config[ 'username' ], $this->_config[ 'password' ] );
    } catch( PDOException $e ) {
    echo __LINE__.$e->getMessage();
    }
    }
    }

    /* Function runQuery
    * Runs a insert, update or delete query
    * @param string sql insert update or delete statement
    * @return int count of records affected by running the sql statement.
    */
    public function runQuery( $sql ) {
    try {
    $count = $this->dbc->exec($sql) or print_r($this->dbc->errorInfo());
    } catch(PDOException $e) {
    echo __LINE__.$e->getMessage();
    }
    return $count;
    }

    /* Function getQuery
    * Runs a select query
    * @param string sql insert update or delete statement
    * @returns associative array
    */
    public function getQuery( $sql ) {
    $stmt = $this->dbc->query( $sql );
    $stmt->setFetchMode(PDO::FETCH_ASSOC);

    return $stmt;
    }


    }
    11 changes: 11 additions & 0 deletions settings.config.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    <?php
    /*
    * Database Connection Settings
    */

    $localhost = array(
    'driver' => 'mysql',
    'host' => 'localhost',
    'dbname' => 'database',
    'username' => 'username',
    'password' => 'password');