Skip to content

Commit

Permalink
added fetchAll and fetchOne api
Browse files Browse the repository at this point in the history
  • Loading branch information
joykumarbera committed Aug 21, 2022
1 parent 4a101fe commit 9b5a7d6
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 4 deletions.
21 changes: 17 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ Select data
```php
$db->query('SELECT * FROM songs')->all()
$db->query('SELECT * FROM songs WHERE id = ?', [1])->one()
$db->findAll('songs');
$db->findOne('songs', ['id' => 1]);
```


Expand Down Expand Up @@ -133,20 +135,31 @@ $db->query('SELECT * FROM songs WHERE id = ?', [1])->one()
$db->lastInsertId()
```

#### Get single record as an array
#### Get single record as an array after query

```php
$db->query($sql, $params = [])->one()
```
#### Get single record as an object
#### Get single record as an object after query

```php
$db->query($sql, $params = [])->oneAsObject()
```
#### Get all records
#### Get all records as an array after query

```php
$db->query($sql, $params = [])->all()

```
#### Get a single record using a table name

```php
$db->findOne($table, $conditions = [], $glue = 'AND', $as = 'object');
```
#### Get all records using a table name

```php
$db->findOne($table, $conditions = [], $glue = 'AND');
```
#### Begin a db transaction

Expand All @@ -157,4 +170,4 @@ $db->query('SELECT * FROM songs WHERE id = ?', [1])->one()

```php
$db->end_transaction()
```
```
58 changes: 58 additions & 0 deletions src/Db.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,64 @@ private function _runQuery($sql, $params)
}
}

/**
* Find one record from a table
*
* @param string $table
* @param array $conditions
* @param string $glue
* @return object|array
*/
public function findOne($table, $conditions = [], $glue = 'AND', $as = 'object')
{
if(!empty($conditions)) {
$where_clause = '';
foreach($conditions as $key => $value) {
$where_clause .= '`' . $key . '`' . ' = ' . '? ' . $glue;
}
$where_clause = rtrim($where_clause, " $glue");
} else {
$where_clause = 1;
}

$sql = "SELECT * FROM `$table` WHERE $where_clause";

$this->_runQuery($sql, array_values($conditions));

if($as == 'object') {
return $this->oneAsObject();
}

return $this->one();
}

/**
* Find all records from a table
*
* @param string $table
* @param array $conditions
* @param string $glue
* @return array
*/
public function findAll($table, $conditions = [], $glue='AND')
{
if(!empty($conditions)) {
$where_clause = '';
foreach($conditions as $key => $value) {
$where_clause .= '`' . $key . '`' . ' = ' . '? ' . $glue;
}
$where_clause = rtrim($where_clause, " $glue");
} else {
$where_clause = 1;
}

$sql = "SELECT * FROM `$table` WHERE $where_clause";

$this->_runQuery($sql, array_values($conditions));

return $this->all();
}

/**
* Get total nunmber of rows affected rows
*
Expand Down

0 comments on commit 9b5a7d6

Please sign in to comment.