Skip to content

Commit

Permalink
update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
h2lsoft committed Apr 21, 2020
1 parent e6e3e59 commit 680b1b9
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 8 deletions.
84 changes: 82 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,90 @@ $ composer require h2lsoft/db-manager

## Basic Usage

See examples directory
```php
use \h2lsoft\DBManager;

$DBM = new DBManager\DBManager();
$DBM->connect('mysql', 'localhost', 'root', '', 'mydatabase');

// execute simple query with binding
$sql = "SELECT Name, SurfaceArea FROM Country WHERE Continent = :Continent AND deleted = 'NO' ORDER BY SurfaceArea DESC LIMIT 3";
$results = $DBM->query($sql, [':Continent' => 'Asia'])->fetchAll();

### License
// or use short version
$sql = $DBM->select("Name, SurfaceArea")
->from('Country')
->where("Continent = :Continent")
->orderBy('SurfaceArea DESC')
->limit(3)
->getSQL();

$DBM->close();
```

## CRUD operations

### INSERT

```php
$values = [];
$values['Name'] = "Agatha Christies";
$values['Birthdate'] = "1890-10-15";

$ID = $DBM->table('Author')->insert($values);
```


### UPDATE

```php
$values = [];
$values['Name'] = "Agatha Christies";
$affected_rows = $DBM->table('Author')->update($values, $ID);
```

### DELETE

```php
$affected_rows = $DBM->table('Author')->delete(["ID = ?", $ID]);
```


## Pagination component

```php
$sql = $DBM->select("*")
->from('Country')
->where("Continent = :Continent")
->getSQL();

$params = [':Continent' => 'Asia'];

$current_page = 1;

// return a complete array
$pager = $DBM->paginate($sql, $params, $current_page, 20);
```

```
Array
(
[total] => 51
[per_page] => 20
[last_page] => 3
[current_page] => 1
[from] => 1
[to] => 20
[page_start] => 1
[page_end] => 3
[data] => Array(
....
)
)
```



## License

MIT. See full [license](LICENSE).
2 changes: 1 addition & 1 deletion examples/1-simple-resultset.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Simple example to fetch data to server
*/

include "../src/DBManager/DBManager.php";
include "../src/DBManager.php";
include "include/connection_init.php";

// normal query => top 3 countries in ASIA by surface area
Expand Down
2 changes: 1 addition & 1 deletion examples/2-crud.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Simple CRUD example
*/

include "../src/DBManager/DBManager.php";
include "../src/DBManager.php";
include "include/connection_init.php";

// insert record soft mode (with stamp)
Expand Down
5 changes: 2 additions & 3 deletions examples/3-pagination.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Pagination example
*/

include "../src/DBManager/DBManager.php";
include "../src/DBManager.php";
include "include/connection_init.php";

$current_page = (!isset($_GET['page'])) ? 1 : $_GET['page'];
Expand All @@ -18,8 +18,7 @@
->where("Continent = :Continent")
->getSQL();

$params = [];
$params[':Continent'] = "Asia";
$params = [':Continent' => 'Asia'];

$pager = $DBM->paginate($sql, $params, $current_page, 20);

Expand Down
2 changes: 1 addition & 1 deletion examples/4-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Utils example
*/

include "../src/DBManager/DBManager.php";
include "../src/DBManager.php";
include "include/connection_init.php";

// alias
Expand Down

0 comments on commit 680b1b9

Please sign in to comment.