https://github.com/kisphp/dbal
Kisphp Database Access Layer
https://github.com/kisphp/dbal
kisphp mysql pdo pdo-wrapper
Last synced: about 1 month ago
JSON representation
Kisphp Database Access Layer
- Host: GitHub
- URL: https://github.com/kisphp/dbal
- Owner: kisphp
- License: mit
- Created: 2017-01-31T20:09:58.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-10-08T17:10:53.000Z (over 8 years ago)
- Last Synced: 2025-02-23T01:34:09.994Z (over 1 year ago)
- Topics: kisphp, mysql, pdo, pdo-wrapper
- Language: PHP
- Homepage:
- Size: 13.7 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# Simple MySQL Database Access Layer
[](https://travis-ci.org/kisphp/dbal)
[](https://codecov.io/gh/kisphp/dbal)
## Installation
Run in terminal
```sh
composer require kisphp/dbal: *
```
## Connect to database
```php
'pdo_mysql',
'host' => 'localhost',
'dbname' => 'test',
'user' => 'root',
'password' => '',
];
$connection = DriverManager::getConnection($connectionParams, $config);
$db = new Database($connection, new DatabaseLog());
```
## Database Insert
> `$db->insert('table_name', 'data array');`
If you need `INSERT IGNORE` syntax, then pass `true` for the third parameter
```php
$db->insert('test_table', [
'column_1' => 'value_1',
'column_2' => 'value_2',
]);
// will return last_insert_id
$insertIgnore = true;
$db->insert(
'test_table',
[
'column_1' => 'value_1',
'column_2' => 'value_2',
],
$insertIgnore
);
// will execute INSERT IGNORE ...
```
## Database update
> `$db->update('table_name', 'data array', 'condition value', 'column name (default=id)');`
```php
$db->update('test_table', [
'column_1' => 'value_1',
'column_2' => 'value_2',
], [
'id' => 1
]);
// will return affected_rows
```
## Get single value
```php
$value = $db->getValue("SELECT column_1 FROM test_table");
// or
$value = $db->getValue("SELECT column_1 FROM test_table WHERE id = :id", [ 'id' => 1 ]);
```
## Get pairs
```php
$pairs = $db->getPairs("SELECT id, column_1 FROM test_table");
// or
$pairs = $db->getPairs("SELECT id, column_1 FROM test_table WHERE column_2 = :condition", [ 'condition' => 'demo' ]);
/*
will result
$pairs = [
'1' => 'c1.1',
'2' => 'c2.1',
'3' => 'c3.1',
];
*/
```
## Get Custom query
```php
$query = $db->query("SELECT * FROM test_table");
// or
$query = $db->query("SELECT * FROM test_table WHERE column = :condition", [ 'condition' => 'demo' ]);
while ($item = $query->fetch(\PDO::FETCH_ASSOC)) {
var_dump($item);
}
```