dev-master
9999999-dev http://panadaframework.com/A standalone Panada database package
MIT License
The Requires
- php >=5.5
The Development Requires
by Iskandar Soesman
A standalone Panada database package
This is A standalone Panada database package., (*1)
Install via composer, (*2)
composer require panada/database
<?php require_once __DIR__.'/vendor/autoload.php'; $db = new Panada\Database\SQL([ 'dsn' => 'mysql:host=127.0.0.1;dbname=panada;port=3306', 'username' => 'root', 'password' => '', 'options' => [ PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8', PDO::ATTR_PERSISTENT => true ]); $insert = $db->insert('account', [ 'user_name' => 'foo', 'email' => 'foo@bar.com', ]);
<?php namespace Controller; class Hello { use \Panada\Resource\Controller; public function index() { $this->db = \Panada\Database\SQL::getInstance(); $query = $this->db->insert('users', [ 'name' => rand(), 'email' => 'budi@budi.com', 'password' => 'password' ]); $data = $this->db->select()->from('users')->getAll(); return 'status insert: '.var_export($query, true).' data: <pre>'.print_r($data, true).'</pre>'; } }
If you have more then one db connection, here's the example:, (*3)
<?php return [ 'default' => [ 'dsn' => 'mysql:host=127.0.0.1;dbname=mydb1;port=3306', 'username' => 'root', 'password' => '', 'options' => [ PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8', PDO::ATTR_PERSISTENT => true ] ], 'db2' => [ 'dsn' => 'mysql:host=127.0.0.1;dbname=mydb2;port=3307', 'username' => 'root', 'password' => '', 'options' => [ PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8', PDO::ATTR_PERSISTENT => true ] ], 'db3' => [ 'dsn' => 'mysql:host=127.0.0.1;dbname=mydb3;port=3308', 'username' => 'root', 'password' => '', 'options' => [ PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf9', PDO::ATTR_PERSISTENT => true ] ] ];
Call the db helper:, (*4)
public function testDB() { $db1 = \Panada\Database\SQL::getInstance(); $db2 = \Panada\Database\SQL::getInstance('db2'); $db3 = \Panada\Database\SQL::getInstance('db3'); }
$query = $this->db->insert('tableName', ['name' => 'jhon', 'email' => 'foo@bar.com']);
SQL output:, (*5)
INSERT INTO tableName (name, email) VALUES ('jhon', 'foo@bar.com')
$query = $this->db->update('tableName', ['name' => 'jhon gmail', 'email' => 'jhon@gmail.com'], ['id' => 6]);
SQL output:, (*6)
UPDATE tableName SET name = 'budi', email = 'budi@budi.com' WHERE id = 6
$data = $this->db->select()->from('users')->getAll();
SQL output:, (*7)
SELECT * FROM users
$data = $this->db->select('id', 'name')->from('users')->getAll(); // or $data = $this->db->select(['id', 'name'])->from('users')->getAll();
SQL output:, (*8)
SELECT id, name FROM users
$data = $this->db->select('COUNT(*)')->from('users')->getVar();
SQL output:, (*9)
SELECT COUNT(*) FROM users
$data = $this->db->select('name')->distinct()->from('users')->limit(10)->getAll();
SQL output:, (*10)
SELECT DISTINCT name FROM users LIMIT 10
Heres an example to insert then fatch some db data:, (*11)
public function testDB() { $this->db = \Panada\Database\SQL::getInstance(); $query = $this->db->insert('users', [ 'name' => rand(), 'email' => 'budi@budi.com', 'password' => 'password' ]); $data = $this->db->select()->from('users')->getAll(); return 'status insert: '.var_export($query, true).' data: <pre>'.print_r($data, true).'</pre>'; }
Go to project root. Run composer install to get PHPUnit Package., (*12)
composer install
Then run the test:, (*13)
./vendor/bin/phpunit
A standalone Panada database package
MIT License