GitHub - Devictorinos/Dbw: working with data base · GitHub
Skip to content

Devictorinos/Dbw

Folders and files

Repository files navigation

Dbw - it`s a FrameWork for work with data bases, based on PDO connection. Dependencies - PHP 5.3.* >=.

For now has capabilities to setup unlimmit connections types to one database or to several databases, also setup database default connection settings inside class and override them if need, when calling the class.

It's very friendly with SQL methods like SELECT, INSERT, UPDATE, DELETE. and has method to convert ASCII chars after fetch, when working with hebrew data base

More features comming soon :)

Getting Started

With override default config method:

  require_once "autoloader.php";
  
  $config  = array('host'    => "localhost",
                 'user'    => "root",
                 'pass'    => "123",
                 'dbname'  => "northwind",
                 'options' => array(
                            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
                            PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8",
                            PDO::ATTR_TIMEOUT => 1000,
                        )
                 );

$db = \DBWork\Dbw::R($config);

With default config:

 $db = \DBWork\Dbw::R();

Letter R() represents Connection Type.

Working with SELECT method

$db = \DBWork\Dbw::R();

$a = 1;
$b = 2;

$sql = "SELECT EmployeeID, LastName, FirstName, BirthDate, Address, City, Region FROM `Employees` 
        WHERE  EmployeeID IN (".$a.",".$b.",6,7)";

$db->select($sql);

for activate debug before execute just add second parameter - true

$db->select($sql, true);

Fetch Methods

------------------------------- For while loops -----------------------------------

$db->select($sql);
$row = $db->oFetch();

$row = $db->fetch() // fetch assoc

$row = $db->oFetch() // fetch object

$row = $db->fetchClass('Class Name') //fetch class

$row = $db->fetchIntoClass(new Class())// fetch into class

$row = $db->fetchClassAfterConstr('Class Name') // fetch to class after constuctor

----------------------- Fetch All Without While Loops -------------------------------

$result = $db->fetchAll()

$result = $db->oFetchAll()

$result = $db->fetchClassAll('Class Name')

$result = $db->fetchClassAllAfterConstr('Class Name')

$result = $db->fetchAllIntoClass(new Class())

Working with UPDATE method

update method expects two parameters, first - Table Name, second associative array, and return affected rows count.

In update method very important to set all where conditions like where, whereIn, whereBetween with number in the end of each condition. Here is an Example:

$arr = [];
$arr['titleOfCourtesy']   = "Mss.";


$result = $db->update('Employees', $arr)
             ->whereBetween1("EmployeeID", 2, 10)
             ->where1("Country", "USA")
             ->exec();

WHERE Conditions

As you can see, each where condition comes with number in the end. it's very important to setup where conditions with numbers, otherwise you will see errors. If you want to pass more than 1 where or whereIn or whereBetween, respectively you can add them with number increment like this :

where1()->where2()->where3();

also you can pass a separator, by default is "=" To pass a differend separator just add third parameter to the where conditions like this :

$arr = [];
$arr['titleOfCourtesy']   = "Mss.";


$result = $db->update('Employees', $arr)
             ->where1("Date", "2014-09-28", ">") //in sql you will se this like : where date > "2014-09-28"
             ->where2("Country", "USA")  //in sql you will se this like : where Country "USA"
             ->exec();

exec() Method

exec method is executing the Query. You only need to add him to the end of all your where conditions, and if you want to debug your Query before execute, just pass - true in exec like this:

$arr = [];
$arr['titleOfCourtesy']   = "Mss.";


$result = $db->update('Employees', $arr)
             ->where1("Date", "2014-09-28", ">") //in sql you will se this like : where Date > "2014-09-28"
             ->where2("Country", "USA", "<>")  //in sql you will se this like : where Country <> "USA"
             ->exec(true);

Working with INSERT method

insert method looks the same as Update, exclude where conditions, he simply doesn't have them insert method expects to parameters , first - Table Name , second associative array, and returning affected rows count. here is an example :

$arr = [];
$arr['FirstName']   = "Vivid";
$arr['LastName']   = "Coches";
$arr['BirthDate']   = date("Y-m-d H:i:s");

$result2 = $db->insert('Employees', $arr);

If you want to debug your query before execute it, just pass third parameter true

insert('Employees', $arr, true);

To get last insert id use $db->getLastInsertId() But always remember, when debug is on, lastInsertId throwing exception,because there are no executed query. here is an example :

$arr = [];
$arr['FirstName']   = "Vivid";
$arr['LastName']   = "Coches";
$arr['BirthDate']   = date("Y-m-d H:i:s");

$result2 = $db->insert('Employees', $arr);
$lastID =  $db->getLastInsertId();

Working with DELETE method

Delete method expects only table name, and then where conditions like in Update method. it's work in the same way as Update.

Returning count of deleted rows. By default limit is set to one.

here is an Example :

 $result3 = $db->delete('Employees')->where1("EmployeeID", 34)->exec();

For Debug just pass true in exec method

 $result3 = $db->delete('Employees')->where1("EmployeeID", 34)->exec(true);

If you want change limit, you can do this with this method setLimit(54)

Here is an Example :

 $db->setLimit(54);
 $result3 = $db->delete('Employees')->where1("EmployeeID", 34)->exec(true);

About

working with data base

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

Contributors

Languages