https://github.com/jaredhowland/database
Simple PDO wrapper for MySQL and SQLite databases
https://github.com/jaredhowland/database
pdo-wrapper wip
Last synced: 10 months ago
JSON representation
Simple PDO wrapper for MySQL and SQLite databases
- Host: GitHub
- URL: https://github.com/jaredhowland/database
- Owner: jaredhowland
- License: mit
- Created: 2016-10-27T20:23:29.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2023-09-14T04:46:22.000Z (over 2 years ago)
- Last Synced: 2024-02-15T04:40:22.115Z (almost 2 years ago)
- Topics: pdo-wrapper, wip
- Language: PHP
- Homepage: https://github.com/jaredhowland/database
- Size: 59.6 KB
- Stars: 3
- Watchers: 4
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Simple PDO Wrapper
A simple PDO-wrapper for accessing MySQL and SQLite databases.
# Installation
Install with composer:
`$ composer require jaredhowland/simple-pdo-wrapper:~2.0`
# Examples
Here are a few examples of how to use this wrapper. More comprehensive documentation is coming later (hopefully).
## Instantiate
```
$db = new \Database\Database();
```
## SQLite Connection
```
$db->driver('sqlite')
->dbPath('sqlite.db')
->connect();
```
## MySQL Connection
```
$db->driver('mysql')
->host('localhost')
->dbName('DB-NAME')
->username('USERNAME')
->password('PASSWORD')
->connect();
```
## SELECT
This example uses `bindValue` and returns an associative array of all the results.
```
$results = $db->select('`column1`', '`column2`')
->from('`table`')
->where("`column1` = :column1 AND `column2` = :column2")
->orderBy('`platform`')
->bind(['column1' => $column1Value, 'column2' => $column2Value])
->fetchAll();
```
## INSERT
```
$db->insert('`table`')
->columns('`column1`', '`column2`')
->values('value1', 'value2')
->execute();
```
## DELETE
```
$db->delete('`table`')
->where("`column` = 'value'")
->execute();
```
## UPDATE
```
$db->update('`table`')
->set("`column` = 'value'")
->where("`column2` = 'value2'")
->execute();
```
## REPLACE
`REPLACE` works exactly like `INSERT`, except that if an old row in the table has the same value as a new row for a `PRIMARY KEY` or a `UNIQUE` index, the old row is deleted before the new row is inserted.
```
$db->replace('`table`')
->columns('`column`', '`column2`')
->values('value1', 'value2')
->execute();
```
## LEFT JOIN
```
$results = $db->select(`table1`.`column`)
->from(`table1`)
->leftJoin(`table2`, `table3`)
->on(`table2`.`column` = `table1`.`column` AND `table3`.`column` = `table1`.`column`)
->fetchAll();
```
## TRUNCATE
```
$db->truncate('`table`');
```
## mysqldump
This method will dump the entire database into the specified file.
```
$db->mysqldump('path/to/dump/file.sql');
```
## Quick Reference
If you want a quick reference on the syntax of various SQL statements, use `sqlRef()`.
```
$db->sqlRef();
```
### Output
```
SELECT:
SELECT `column1`, `column2` FROM `table` WHERE `column1` = 'value' GROUP BY `column1` ORDER BY `column2` LIMIT 2
INSERT:
INSERT INTO `table` (`column1`, `column2`) VALUES (`value1`, `value2`) ON DUPLICATE KEY UPDATE `column1` = 'value'
REPLACE:
REPLACE INTO `table` (`column1`, `column2`) VALUES ('value1', 'value2')
DELETE:
DELETE FROM `table` WHERE `column` = 'value' ORDER BY `column` LIMIT 2
UPDATE:
UPDATE `table` SET `column1` = 'value1', `column2` = 'value2' WHERE `column1` = 'value1' ORDER BY `column2` LIMIT 2
LEFT JOIN:
SELECT `table`.`column` FROM `table1` LEFT JOIN (`table2`, `table3`) ON (`table2`.`column` = `table1`.`column` AND `table3`.`column` = `table1`.`column`)
```