This project provides a full extension for PHP's PDO (PHP Data Objects) class designed for ease-of-use and saving development time/effort. This is achived by providing methods - delete, insert, select, select a row, update and other PDO property - for quickly building common SQL statements, handling exceptions when SQL errors are produced, and automatically returning results/number of affected rows for the appropriate SQL statement types.
- Connection Settings
- Installation
- Methods
- Select
- Fetch Modes
- Single Row
- Fetch Modes
- Insert
- Udate
- Delete
- Query
- Stored Procedure
- Select
- First of all, Edit the database settings in the Config/Database.php
<?php
return [
//Database Connection parameters
'connections' => [
'mysql' => array(
'read' => array(
'host' => '127.0.0.1', //localhost
),
'write' => array(
'host' => '127.0.0.1' //localhost
),
'port' => '3306',
'driver' => 'mysql',
'database' => 'test', //database name
'username' => 'root', //database username
'password' => '', //database user password
'charset' => 'utf8', //default charset
'collation' => 'utf8_general_ci', //charset collection
'persistent'=> false,
'prefix' => ''
),
],
];
- Require the class in your project.
require_once('Config/Config.php');
require_once('vendor/autoload.php');
- Create the instance Database Class
You can look php namespace usage. 🔗 PHP Manual-Using namespaces
use Bin\System\Database as DB;
$DB=new DB();
-
Once you have configured your database connection, you may run methods using the DB facade.
$table = 'users';
$results=DB::select($table);
The select method will always return an array of results.
- If you want get some field in table.
$table = 'users';
$fields = array('Name','Lastname');
$result=DB::select($table,$fields);
- Default fetch mode is PDO::FETCH_ASSOC. You can change fetch mode.
- 🔗 PDOStatement::fetch
DB::setFetchMode(PDO::FETCH_OBJ);
//"PDO::FETCH_OBJ: returns an anonymous object with property names that correspond to the column names returned in your result set"
$fields = array('Name','Lastname');
$result=DB::select($table,$fields);
-
⚠️ setFetchMode method must be used before select, row and query methods.
//row method parameters are $table, $values, $params, $options
//$values and $options can be null,
$table = 'users';
$params = array('Id' => 3);
$result = DB::row($table, null, $params);
- If you want get some field in table.
$table = 'users';
$values = array('Id','Name','Lastname');
$params = array('Id' => 3);
$result = DB::row($table, $values, $params);
- Default fetch mode is PDO::FETCH_ASSOC. You can change fetch mode.
- 🔗 PDOStatement::fetch
DB::setFetchMode(PDO::FETCH_OBJ);
$table = 'users';
$params = array('Id' => 3);
$result = DB::row($table, null, $params);
-
⚠️ setFetchMode method must be used before select, row and query methods.
$params = ['Name' => 'Fatih',
'Lastname' => 'ÖZTÜRK',
'Age' => 29,
'Birthdate' => '1985-03-15'];
$result = DB::insert('users', $params);
if($result){
echo "Success message";
}
-
⚠️ The array keys must be same table coloumn name.
$params = ['Name' => 'Fatih',
'Lastname' => 'ÖZTÜRK',
'Age' => 29,
'Birthdate' => '1985-03-15',
'Where' => ['Id' => 1]];
$result = DB::update('users', $params);
if ($result) {
echo "Success message";
}
-
⚠️ You must set where clause like 'Where'=>['Id' => 1]
$params = ['Id'=>1];
$result = DB::delete('users', $params);
-
If you can use a subquery or join query, this method is simply fetch your query.
$sql="SELECT * FROM users";
$result=DB::query($sql);
Comming soon