PHP Classes

File: Usage-and-examples

Recommend this page to a friend!
  Classes of Yazan Tommalieh   DB Accelerator   Usage-and-examples   Download  
File: Usage-and-examples
Role: Example script
Content type: text/plain
Description: How to use
Class: DB Accelerator
Map MySQL table records to objects using MySQLi
Author: By
Last change: fix
Date: 9 years ago
Size: 2,404 bytes
 

Contents

Class file image Download
//this class reflects an already created db table
//you can also use constuctor to set initial data if you want
Class User
{
    public $Id; //auto increment in db
    public $Username;
    public $Password;
}

//initiate Accelerator
require 'Accelerator.php'; //load class
$db = new Accelerator();

//create db if not exists
$dbName = 'testDB';
$db->Create($dbName);

//connect to db testDB
$db->Connect('host', 'user', 'pass', $dbName);

//create user table if not exists
$usersTable = "CREATE TABLE IF NOT EXISTS User(
                    Id int auto_increment,
                    Username varchar(128),
                    Password varchar(128),
                    primary key(Id))";
                   

$db->Execute($usersTable);

//add new user record
$user = new User();
$user->Username = 'test';
$user->Password = '123456';
$db->add($user);

//select * from User
$users = $db->User->All();

//select users count
$count = $db->User->Count();

//select user where username = 'test', returns false if nothing found
$user = $db->User->Select(array("Username" => "test"));

//update user
$user->Username = 'newTest';
$db->User->Update($user);

//delete user
$db->User->Delete($user);

//select user where username = 'test' and password='123456'
$user = $db->User->Select(array("Username" => "test", "Password" => "123456));

//select user where username = 'test' or username = 'test1'
$user = $db->User->Select(array("Username" => "test", "Username" => "test1", "mode" => "or"));

//select user where username = 'test' or username = 'test1' order by username desc
$user = $db->User->Select(array("Username" => "test", "Username" => "test1", "mode" => "or", order=> array("Username" => "DESC")));

//select user where username = 'test' or username = 'test1' order by username desc and password asc
$user = $db->User->Select(array("Username" => "test", "Username" => "test1", "mode" => "or", order=> array("Username" => "DESC", "Password" => "ASC")));

//select top 5 users
$user = $db->User->Select(array("start" => "0", "take" => "5"));

//select 5 users starting from position 10, ordered by username desc
$user = $db->User->Select(array("start" => "10", "take" => "5", "order" => array("Username" => "DESC")));

//custom and advanced select query
$sql = "select * from User where Username like '%t%'";
$user = $db->User->Result($sql);

//execute any query
$sql = "Drop table User";
$db->Execute($sql);