Skip to content

Commit 473686d

Browse files
author
modbot
committed
Update 1.1
Added functions - FetchAll($query) - FetchAll_Safe($query)
1 parent bee5722 commit 473686d

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

README.md

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,31 @@
1-
Database
1+
# Database
22

33
An easy to use class for Database queries in PHP.
44

5+
- [API](#api)
6+
- [Examples](#data-examples)
7+
- [Install](#installation)
8+
9+
## API
10+
```php
11+
Database::connect($db='test',$pass='',$user='root',$host='localhost',$type='mysql');
12+
Database::query($query, $params = array());
13+
Database::fetchAll($query);
14+
Database::fetchAll_safe($query);
15+
Database::fetch_object($query);
16+
Database::fetch_safe_object($query);
17+
Database::num_rows($query);
18+
```
19+
20+
See Below for usage.
21+
522
### Connecting
623
```php
724
Database::connect('database','password','username','host');
825
```
926

1027
Note the reverse parameters. We do this because of the ommitable variables.
1128

12-
**Defaults:**
13-
dbhost = localhost
14-
dbuser = root
15-
dbpass = root
16-
dbname = test
17-
1829
### Query
1930
```php
2031
$query = Database::query("SELECT * FROM table WHERE id = ?", [$_GET['id']]);

src/Database.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,24 @@ public static function fetch_safe_object($query)
3131
$res = self::walk_recursive($query->fetch(PDO::FETCH_OBJ), 'htmlspecialchars');
3232
return $res;
3333
}
34+
public static function fetchAll($query)
35+
{
36+
return $query->fetchAll(PDO::FETCH_OBJ);
37+
}
38+
public static function fetchAll_safe($query)
39+
{
40+
$res = self::walk_recursive($query->fetchAll(PDO::FETCH_OBJ), 'htmlspecialchars');
41+
return $res;
42+
}
3443
public static function num_rows($query)
3544
{
3645
return $query->rowCount();
3746
}
47+
public static function insert_id()
48+
{
49+
$id = self::$pdo->lastInsertId();
50+
return $id;
51+
}
3852

3953
public static function walk_recursive($obj, $closure)
4054
{
@@ -64,4 +78,4 @@ public static function walk_recursive($obj, $closure)
6478
return $closure($obj);
6579
}
6680
}
67-
}
81+
}

0 commit comments

Comments
 (0)