Coder Social home page Coder Social logo

simple-database-php-class's Introduction

How to use it

Get new instance like this

<?php
require 'Db.php';
$db = new Db( 'mysql', 'hostName', 'databaseName', 'databaseUser'[, 'databaseUserPassword'] );

Or set config

<?php
require 'Db.php';
Db::config( 'driver',   'mysql' );
Db::config( 'host',     'hostName' );
Db::config( 'database', 'databaseName' );
Db::config( 'user',     'databaseUser' );
Db::config( 'password', 'databaseUserPassword' );

And later get instance easily

<?php
$db = Db::instance();

Basic methods

<?php
$db->raw( 'SELECT * FROM Table' ); // Execute raw SQL query (don't forget to secure your SQL)

$db->query( 'SELECT * FROM Table WHERE id = :id', array( 'id' => 1 ) ); // Prepare and execute SQL

$db->select( 'Table' ); // Select everything (*) from Table
$db->select( 'Table', 'id' ); // Select id column
$db->select( 'Table', array( 'id', 'field' ) ); // Select id & field columns
$db->select( 'Table', '*', array( // Select everything where field1 = $value1 AND field2 = $value2
    'field1' => $value1,
    'field2' => $value2
) );
$db->select( 'Table', '*', array( // Complex select where N.B: you can't mix ? and named placeholder
    'field1 >= ? AND field1 <= ? '                     => array(42, 100),
    'field2 LIKE ?'                                    => '%test%',
    'field3 = :value OR CHAR_LENGTH(field3) < :length' => array(
        'value'  => $value,
        'length' => 10
    )
) );

CRUD methods

<?php
$db->create( 'Table', array( 'field' => 'data' ) ); // Insert into Table

$db->read( 'Table', 1 ); // Select from Table where primary key = 1
$db->read( 'Table', 'data', 'field' ); // Select from Table where field = 'data'

$db->update( 'Table', 'field', 'test', 1 ) // Update field to 'test' in Table where primary key = 1
$data = array( 'field' => 'test' ); // Passing data as array shift next arguments
$db->update( 'Table', $data, 1 ) // Equivalent to previous line
$db->update( 'Table', $data, 'test', 'data' ) // Update field(s) containing 'data' to  'test'

$db->delete( 'Table', 1 ) // Delete Table row where primary key = 1
$db->delete( 'Table', 'test', 'field' ) // Delete Table row where field = 'test'

Fetch methods

<?php
$db->read( 'Table', 1 )->fetch(); // fetch returns objects (stdClass by default)
$db->read( 'Table', 1 )->fetch( 'Class' ); // But you can specify a class
$select = $db->select( 'Table' );
while ( $row = $select->fetch() ) // It's a row fetching method
  echo $row->field;

$select = $db->select( 'Table' );
while ( $row = $select->fetch( false ) ) // returns associative array
  echo $row[ 'field' ];

$db->select( 'Table' )->all(); // all returns all rows (as ArrayObject)
$db->select( 'Table' )->all( 'Class' ); // You also can specify row class

$db->select( 'Table', 'id' )->column( 'id' ); // column returns only values (as an array)

Various methods

<?php
$db->key( 'Table' ); // Get Table primary key (id)
$db->quote( $value ); // Get quote protected value
$db->create( 'Table', array( 'field' => 'data' ) ); // Add a row, then
$db->id(); // Get last inserted id

Config tricks

Db::config method is a getter and a setter

<?php
Db::config( 'host', 'hostName' ); // Two argument -> setter
Db::config( array(    // One array argument -> setter
  'user'       => 'databaseUser'
  , 'password' => 'databaseUserPassword'
) );

Db::config( 'host' ); // One argument -> getter

You can store what you want in config (only driver, host, database, user, password & fetch are reserved)

<?php
Db::config( 'salt', 'p*d5h|zpor7spm#i' ); // set a salt to reuse it later
$user = array(
  'login'      => $login
  , 'password' => md5( Db::config( 'salt' ) . $password ) //  Hash password
);
$userId = $db->create( 'User', $user )->id(); // Save new user

By default fetch method return stdClass, but you can customize globaly

<?php
Db::config( 'fetch', 'Class' ); // Set class to use for object
$db->read( 'Table', 1 )->fetch(); // Methods fetch and all now return Class object(s)
$db->select( 'Table' )->all();
$db->read( 'Table', 1 )->fetch( 'OtherClass' ); // You still can override it

Read, update and delete methods automatically guess which primary key to use, but you can set/override it manually

<?php
Db::config( 'databaseName.Table:PK', 'field' ); // Manually set 'primary' key of table to field
$db->read( 'Table', 'test' ); // Now, this select from Table where field = 'test'

simple-database-php-class's People

Contributors

adriengibrat avatar

Watchers

James Cloos avatar TJ Nine avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.