Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/neutron-pro/neutronstars-database-sql
https://github.com/neutron-pro/neutronstars-database-sql
Last synced: 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/neutron-pro/neutronstars-database-sql
- Owner: Neutron-Pro
- License: apache-2.0
- Created: 2021-01-17T01:32:34.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2021-06-30T22:33:07.000Z (over 3 years ago)
- Last Synced: 2024-08-11T23:48:05.144Z (5 months ago)
- Language: PHP
- Size: 45.9 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.MD
- License: LICENSE
Awesome Lists containing this project
README
# Database Library For PHP
# Installation
```
composer require neutronstars/database-sql
```
```json
{
"require": {
"neutronstars/database-sql": ">=1.0.*"
}
}
```# Importation
```php
'127.0.0.1',
'port' => 3306,
'user' => 'root',
'password' => '',
'charset' => 'utf8mb4'
];
$database = new Database('database_name', $options);
```# SQL Request Samples
## SELECT
```sql
SELECT * FROM table;
```
```php
$results = $database->query('table')
->select('*')
->getResults();
```---
```sql
SELECT name,email FROM table where id=4;
```
```php
$result = $database->query('table')
->select('name', 'email')
->where('id=:id')
->setParameters([
':id' => 4
])
->getResult();
```---
```sql
SELECT * FROM table LIMIT 10 OFFSET 20;
SELECT * FROM table LIMIT 20,10;
```
```php
$results = $database->query('table')
->select('*')
->limit(10, 20)
->getResults();
```---
```sql
SELECT * FROM table ORDER BY points DESC;
```
```php
use NeutronStars\Database\Query;$results = $database->query('table')
->select('*')
->orderBy('points', Query::ORDER_BY_DESC)
->getResults();
```---
```sql
SELECT * FROM table GROUP BY name;
```
```php
$results = $database->query('table')
->select('*')
->groupBy('name')
->getResults();
```---
```sql
SELECT * FROM table1 t1 LEFT JOIN table2 t2 ON t1.id = t2.id;
```
```php
use NeutronStars\Database\Query;$results = $database->query('table t1')
->select('*')
->join('table2 t2', 't1.id = t2.id', Query::LEFT_JOIN)
->getResults();
```---
## DELETE
```sql
DELETE FROM table WHERE id=4;
```
```php
$database->query('table')
->delete()
->where('id=:id')
->setParameters([
':id' => 4
])
->execute();
```---
## UPDATE
```sql
UPDATE table SET name='Doe' WHERE lastname='John';
```
```php
$database->query('table')
->update('name=:name')
->where('lastname=:lastname')
->setParameters([
':name' => 'Doe',
':lastname' => 'John'
])
->execute();
```---
## INSERT INTO
```sql
INSERT INTO table (name,lastname) VALUES (:name1,:lastname1), (:name2,:lastname2);
```
```php
$database->query('table')
->insertInto('name,lastname', ':name1,:lastname1', ':name2,:lastname2')
->setParameters([
':name1' => 'Doe',
':lastname1' => 'John',
':name2' => 'Doe',
':lastname2' => 'Jane'
])
->execute();
``````sql
INSERT INTO table (name,lastname) VALUES (:name,:lastname) ON DUPLICATE KEY UPDATE name=:name, lastname=:lastname;
```
```php
$database->query('table')
->insertInto('name,lastname', ':name,:lastname')
->onDuplicateKeyUpdate('name=:name,lastname=:lastname')
->setParameters([
':name' => 'Doe',
':lastname' => 'John'
])
->execute();
```---
## SUB REQUEST
```sql
SELECT * FROM (SELECT * FROM table) AS test;
```
```php
use NeutronStars\Database\QueryBuilder;$builder = (new QueryBuilder('table'))->select('*');
$results = $database->withQuery($builder, 'test')
->getResults();
``````sql
SELECT * FROM users u LEFT JOIN (SELECT * from test) AS t ON t.id=u.id WHERE u.points>5;
```
```php
use NeutronStars\Database\QueryBuilder;
use NeutronStars\Database\Query;$builder = (new QueryBuilder('test'))->select('*');
$results = $database->query('users u')
->select('*')
->joinQuery($builder, 't', 't.id=u.id', Query::LEFT_JOIN)
->where('u.points>:points')
->setParameters([
':points' => 5
])
->getResults();
```---
## UNION
```sql
SELECT u.* FROM users u UNION ALL SELECT p.* FROM profile p;
``````php
$results = $database->query('users')
->select('u.*')
->union('profile p', true)
->select('p.*')
->getResults();
```