PHP code example of chillerlan / php-database
1. Go to this page and download the library: Download chillerlan/php-database library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
chillerlan / php-database example snippets
$options = new DatabaseOptions;
$options->database = 'whatever';
$options->username = 'user';
$options->password = 'supersecretpassword';
$options = new DatabaseOptions([
'database' => 'whatever',
'username' => 'user',
'password' => 'supersecretpassword',
]);
$mysql = new MySQLiDrv($options, $cache, $log);
$mysql->connect();
// a raw query using the driver directly
$result = $mysql->raw('SELECT * FROM sometable');
$querybuilder = new QueryBuilder($mysql, $log)
$result = $querybuilder->select->from(['sometable'])->query();
$options->driver = MySQLiDrv::class;
$db = new Database($options);
$db->connect();
$result = $db->raw('SELECT * FROM sometable');
// is equivalent to
$result = $db->select->from(['sometable'])->query();
$conn->create
->database('test')
->ifNotExists()
->charset('utf8mb4_bin')
->query();
$conn->create
->table('products')
->ifNotExists()
->int('id', 10, null, false, 'UNSIGNED AUTO_INCREMENT')
->tinytext('name', null, false)
->varchar('type', 20)
->decimal('price', '9,2', 0)
->decimal('weight', '8,3')
->int('added', 10, 0, null, 'UNSIGNED')
->primaryKey('id')
->query();
$conn->insert
->into('products')
->values(['name' => 'product1', 'type' => 'a', 'price' => 3.99, 'weight' => 0.1, 'added' => time()])
->query();
$values = [
['name' => 'product2', 'type' => 'b', 'price' => 4.20, 'weight' => 2.35, 'added' => time()],
['name' => 'product3', 'type' => 'b', 'price' => 6.50, 'weight' => 1.725, 'added' => time()],
];
$conn->insert
->into('products')
->multi($values);
$values = [
['product4', 'c', 3.99, 0.1,],
['product5', 'a', 4.20, 2.35,],
['product6', 'b', 6.50, 1.725,],
];
$conn->insert
->into('products')
->values([['name' => '?', 'type' => '?', 'price' => '?', 'weight' => '?', 'added' => '?']])
->callback($values, function($row){
return [
$row[0],
$row[1],
floatval($row[2]),
floatval($row[3]),
time(),
];
});
$result = $conn->select
->cols([
'uid' => ['t1.id', 'md5'],
'productname' => 't1.name',
'price' => 't1.price',
'type' => ['t1.type', 'upper'],
])
->from(['t1' => 'products'])
->where('t1.type', 'a')
->orderBy(['t1.price' => 'asc'])
->query('uid')
->toArray();
$db->update
->table('table_to_update')
->set(['col_to_update' => 'val1'])
->where('row_id', 1)
->query();
$values = [
// [col_to_update, row_id]
['val1', 1],
['val2', 2],
['val3', 3],
];
$db->update
->table('table_to_update')
->set(['col_to_update' => '?'], false) // disable value binding here
->where('row_id', '?', '=', false) // also disable binding here
->multi($values);
$values = $result->map(function($row){
// ...
return [
$row->id,
$row->name('trim'),
// ...
];
});
$result1 = new Result([['id' => 1]]);
$result2 = new Result([['id' => 2]]);
$result1->__merge($result2);
var_dump($result1->toArray());
// -> [['id' => 1], ['id' => 2]]
var_dump($result1->reverse()->chunk(1)[0]);
// -> [['id' => 2]]
json
{
": "^7.4 || ^8.0",
"chillerlan/php-database": "dev-main"
}
}
array(2) {
'c4ca4238a0b923820dcc509a6f75849b' =>
array(4) {
'uid' =>
string(32) "c4ca4238a0b923820dcc509a6f75849b"
'productname' =>
string(8) "product1"
'price' =>
string(4) "3.99"
'type' =>
string(1) "A"
}
'e4da3b7fbbce2345d7772b0674a318d5' =>
array(4) {
'uid' =>
string(32) "e4da3b7fbbce2345d7772b0674a318d5"
'productname' =>
string(8) "product5"
'price' =>
string(4) "8.19"
'type' =>
string(1) "A"
}
}