CRUD library for MySQL with PDO
$ composer create-project devstackgroup/cbase --stability=dev
# config/bootstrap.php
<?php
return new PDO(
'mysql:host=127.0.0.1;dbname=dbname',
'dbuser',
'dbpassword'
);
127.0.0.1
- host addressdbname
- database namedbuser
- database usernamedbpassword
- database password
<?php
require 'vendor/autoload.php';
use CBase\Query\Query;
$db = new Query(require_once 'config/bootstrap.php');
$db->setTable('test');
$db->create([
'field' => 1
]);
$db->close();
Read All with order by id DESC and limit 2 to assoc array
<?php
require 'vendor/autoload.php';
use CBase\Query\Query;
$db = new Query(require_once 'config/bootstrap.php');
$db->setTable('test');
$data = $db->read(['field'])
->orderBy([
'id' => 'DESC'
])
->limit(2)
->get([
'all' => true,
'fetch' => 'assoc'
]);
var_dump($data);
$db->close();
Read All to array
<?php
require 'vendor/autoload.php';
use CBase\Query\Query;
$db = new Query(require_once 'config/bootstrap.php');
$db->setTable('test');
$data = $db->read()
->get();
foreach ($data as $value) {
var_dump($value);
}
$db->close();
<?php
require 'vendor/autoload.php';
use CBase\Query\Query;
$db = new Query(require_once 'config/bootstrap.php');
$db->setTable('test');
$db->update([
'field' => 2
])
->where([
'id' => 1
])
->exec();
$db->close();
<?php
require 'vendor/autoload.php';
use CBase\Query\Query;
$db = new Query(require_once 'config/bootstrap.php');
$db->setTable('test');
$db->delete()
->where([
'id' => 1
])
->exec();
$db->close();