https://github.com/sinri/sinri-database-agent
Provide a old-style alike quick SQL handler for PHP
https://github.com/sinri/sinri-database-agent
Last synced: 8 months ago
JSON representation
Provide a old-style alike quick SQL handler for PHP
- Host: GitHub
- URL: https://github.com/sinri/sinri-database-agent
- Owner: sinri
- License: mit
- Created: 2017-03-21T07:38:19.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2017-03-22T14:42:53.000Z (about 9 years ago)
- Last Synced: 2024-03-14T20:21:43.881Z (about 2 years ago)
- Language: PHP
- Size: 21.5 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# SinriDatabaseAgent



Provide a old-style alike quick SQL handler for PHP.
With this toolkit developers could be free from writing fundamental code for database.
## Composer
composer require sinri/sinri-database-agent
## Usage
1. Include the `autoload.php` into your PHP project.
2. Declare what you need for convenience.
```PHP
// PDO
use sinri\SinriDatabaseAgent\SinriPDO;
$db=new SinriPDO($params);
// MySQLi
use sinri\SinriDatabaseAgent\SinriMySQLi;
$db=new SinriMySQLi($params);
```
3. General Method
If the following SQL
`SELECT X,Y FROM T WHERE KEY>0;`
refers to this records:
KEY
X
Y
1
x1
y1
2
x2
y2
```PHP
$sql="SELECT X,Y FROM T WHERE KEY>0;";
$db->getAll($sql);
// return [["X"=>"x1","Y"=>"y1"],["X"=>"x2","Y"=>"y2"]]
$db->getRow($sql);
// return ["X"=>"x1","Y"=>"y1"]
$db->getCol($sql);
// return ["x1","x2"]
$db->getOne($sql);
// return "x1"
```
If you need to do some modification.
```PHP
$sql="INSERT INTO...";
$db->insert($sql);
// return the last inserted record's PK value.
$sql="UPDATE ...";// or DELETE, etc.
$db->exec($sql);
// return affected row count, or FALSE on failure.
```
You may want to use transactions.
```PHP
$db->beginTransaction();
// do some work
$db->inTransaction();
// return if this line is in transaction
if($has_error){
$db->rollBack();
}else{
$db->commit();
}
```
## For Developers
* Use Code Rule `PSR2`, just run `./PSR2.sh` before commit and push if `phpcs` and `phpcbf` is correctly installed.
* Run `php test/test.php` to do Unit Test after correcting `test\config.php` settings.
### Check
phpcs --report=full --standard=PSR2 --ignore=vendor .
phpcs --report=summary --standard=PSR2 --ignore=vendor .
### Correct
phpcbf --report=full --standard=PSR2 --ignore=vendor .
## License
SinriDatabaseAgent is published under MIT License.