smplPDO – MySql Database Helper Class (Database Abstractions)

smplPDO is a simple and light-weight class written to extend PHP Data Objects (PDO) with extremely useful features. Shorthand methods packed with it can reduce the amount of duplicate code and increase readability of your scripts as well as improve security and performance with automatically preparing & executing prepared statements.

With smplPDO, you can write fully functional database driven PHP apps with ZERO knowledge of SQL .

// Very simple user authentication example with smplPDO.

$email = array( 'email'=>$_POST['email'] );

// Check if a user exists with the submitted email:
if( ! $db->exists( 'users', $email ) ) 
  exit('User not found!');

// If user is found, check if submitted password is correct:
else if( $db->get_var( 'users', $email, 'password' ) != md5( $_POST['pwd'] ) ) 
  exit('Wrong Password');

// User found & password is correct, so let's welcome with firstname:
echo 'Welcome, ' . $db->get_var( 'users', $email, 'firstname' );

Check out documentation for more code examples.

Main Features

  • Lightweight: 500 lines of clean, commented code.
  • Uses PHP Data Objects (PDO) with best practises.
  • Automatically Creates Prepared Statements.
  • Easy Insert, Update, Delete Methods.
  • Select Rows, Columns, Variables with one-liners.
  • Fully Documented.
  • Continuous Support.

Usage

// Include class file in your script.
require('/path/to/smplPDO.php');

$db_host = 'localhost';
$db_name = 'database';
$db_user = 'db_username';
$db_pass = 'db_password';

// init the class same as PDO
$db = new smplPDO( "mysql:host=$db_host;dbname=$db_name", $db_user, $db_pass );

// INSERT, UPDATE, DELETE with prepared statements:
$db->insert( 'table', array( 'name'=>'John Doe', 'age'=>28 ) );
$db->update( 'table', array( 'age'=>29 ), array( 'name'=>'John Doe' ) );
$db->delete( 'table', array( 'name'=>'John Doe' ) );

// SELECT all, single row, single column and single variable:
$db->get_all( 'table', array( 'age'=>22 ) );
$db->get_row( 'table', array( 'name'=>'John Doe' ) );
$db->get_col( 'table', array( 'age'=>28 ), array('name') );
$db->get_var( 'table', array( 'name'=>'John Doe' ) );

// Check if a record exists: 
if( $db->exists( 'table', array( 'name'=>'John Doe' ) ) ) echo 'Record exists!';

// Get the count of matching records:
$db->get_count( 'table', array( 'age'=>22 ) );

// Print out all necessary properties:
$db->debug();

Download smplPDO – MySql Database Helper Class (Database Abstractions)

Leave a Reply

Your email address will not be published. Required fields are marked *