Skip to content

Commit bef7db2

Browse files
author
modbot
committed
Update 1.2
Added Functions: - getPdo() - setPdo($query) - setPdo($query) - fetch_assoc($query) - fetch_safe_assoc($query)
1 parent 473686d commit bef7db2

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@ An easy to use class for Database queries in PHP.
99
## API
1010
```php
1111
Database::connect($db='test',$pass='',$user='root',$host='localhost',$type='mysql');
12+
Database::getPdo();
13+
Database::setPdo($db);
1214
Database::query($query, $params = array());
1315
Database::fetchAll($query);
1416
Database::fetchAll_safe($query);
17+
Database::fetch_assoc($query);
18+
Database::fetch_safe_assoc($query);
1519
Database::fetch_object($query);
1620
Database::fetch_safe_object($query);
1721
Database::num_rows($query);
@@ -26,6 +30,21 @@ Database::connect('database','password','username','host');
2630

2731
Note the reverse parameters. We do this because of the ommitable variables.
2832

33+
### PDO Objects
34+
35+
If you want to use an existing PDO object to connect:
36+
37+
```php
38+
$db = new PDO; # Use Connection details
39+
Database::setPdo($db); # Pass the object
40+
```
41+
42+
If you want to use this database's PDO object to pass to other objects:
43+
44+
```php
45+
$db = Database::getPdo(); # Returns PDO object after connect() is called
46+
```
47+
2948
### Query
3049
```php
3150
$query = Database::query("SELECT * FROM table WHERE id = ?", [$_GET['id']]);

src/Database.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ public static function connect($db='test',$pass='',$user='root',$host='localhost
1111
echo 'ERROR: ' . $e->getMessage();
1212
}
1313
}
14+
public static function setPdo($obj)
15+
{
16+
self::$pdo = $obj;
17+
}
18+
public static function getPdo()
19+
{
20+
return self::$pdo;
21+
}
1422
public static function query($query, $params = array())
1523
{
1624
self::$query = self::$pdo->prepare($query);
@@ -22,6 +30,15 @@ public static function query($query, $params = array())
2230

2331
return self::$query;
2432
}
33+
public static function fetch_assoc($query)
34+
{
35+
return $query->fetch(PDO::FETCH_ASSOC);
36+
}
37+
public static function fetch_safe_assoc($query)
38+
{
39+
$res = self::walk_recursive($query->fetch(PDO::FETCH_ASSOC), 'htmlspecialchars');
40+
return $res;
41+
}
2542
public static function fetch_object($query)
2643
{
2744
return $query->fetch(PDO::FETCH_OBJ);
@@ -78,4 +95,4 @@ public static function walk_recursive($obj, $closure)
7895
return $closure($obj);
7996
}
8097
}
81-
}
98+
}

0 commit comments

Comments
 (0)