Skip to content

Commit bee5722

Browse files
author
modbot
committed
Initial Release
This is the first release.
1 parent b99a2e6 commit bee5722

File tree

3 files changed

+171
-0
lines changed

3 files changed

+171
-0
lines changed

README.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
Database
2+
3+
An easy to use class for Database queries in PHP.
4+
5+
### Connecting
6+
```php
7+
Database::connect('database','password','username','host');
8+
```
9+
10+
Note the reverse parameters. We do this because of the ommitable variables.
11+
12+
**Defaults:**
13+
dbhost = localhost
14+
dbuser = root
15+
dbpass = root
16+
dbname = test
17+
18+
### Query
19+
```php
20+
$query = Database::query("SELECT * FROM table WHERE id = ?", [$_GET['id']]);
21+
```
22+
23+
This is a query with bind parameters.
24+
First argument is the statement, second argument is an array of parameters (optional)
25+
26+
Note: We passed the query into a variable for later re-use.
27+
28+
### Fetch and **Safe Fetch**
29+
This is regular returned object. You still need to apply htmlspecialchars yourself.
30+
```php
31+
$table = Database::fetch_object($query);
32+
```
33+
34+
This is safe returned object. htmlspecialchars is applied to all the objects's properties.
35+
```php
36+
$table = Database::fetch_safe_object($query);
37+
```
38+
39+
### Num Rows
40+
```php
41+
Database::num_rows($query); # Equivalent of $pdo->rowCount();
42+
```
43+
44+
### Data Examples
45+
```php
46+
# Loop Objects
47+
while($entry = Database::fetch_safe_object($query))
48+
{
49+
# Because of fetch_safe_object we don't need to apply htmlspecialchars
50+
echo '<a href="page?id='.$entry->id.'">'.$entry->name.'</a><br />';
51+
}
52+
# Single Object
53+
$entry = Database::fetch_safe_object($query);
54+
echo $entry->name;
55+
56+
# Loop Objects Using Foreach instead with Fetchall
57+
foreach(Database::fetchAll_safe($query) as $entry)
58+
{
59+
# Because of fetchAll_safe we don't need to apply htmlspecialchars
60+
echo '<a href="page?id='.$entry->id.'">'.$entry->name.'</a><br />';
61+
}
62+
# Single Object
63+
$entry = Database::fetchAll_safe($query);
64+
echo $entry[0]->name;
65+
```
66+
67+
## Installation
68+
69+
via Composer:
70+
```json
71+
{
72+
"require": {
73+
"modularr/database": "1.*"
74+
}
75+
}
76+
```
77+
Then run:
78+
79+
composer update
80+
81+
Or install like so:
82+
83+
composer require modularr/database
84+
85+
make sure you have:
86+
```php
87+
require 'vendor/autoload.php';
88+
```
89+
90+
Manual:
91+
92+
1. Download [Release](https://github.com/Modularr/Database/releases) Or copy file manually
93+
2. Include **Database.php** (found under **src/**)
94+
3. Check out the example

composer.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "modularr/database",
3+
"type": "library",
4+
"description": "Simple Database Library",
5+
"keywords": ["modularr","database","mysql","pdo"],
6+
"license": "UNLICENSE",
7+
"autoload": {
8+
"classmap": ["src/", "src/Database.php"]
9+
}
10+
}

src/Database.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
class Database
3+
{
4+
public static $pdo,$query,$res;
5+
public static function connect($db='test',$pass='',$user='root',$host='localhost',$type='mysql')
6+
{
7+
try {
8+
self::$pdo = new PDO("$type:host=$host;dbname=$db", $user, $pass);
9+
self::$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
10+
} catch(PDOException $e) {
11+
echo 'ERROR: ' . $e->getMessage();
12+
}
13+
}
14+
public static function query($query, $params = array())
15+
{
16+
self::$query = self::$pdo->prepare($query);
17+
if(!empty($params)) {
18+
self::$query->execute($params);
19+
} else {
20+
self::$query->execute();
21+
}
22+
23+
return self::$query;
24+
}
25+
public static function fetch_object($query)
26+
{
27+
return $query->fetch(PDO::FETCH_OBJ);
28+
}
29+
public static function fetch_safe_object($query)
30+
{
31+
$res = self::walk_recursive($query->fetch(PDO::FETCH_OBJ), 'htmlspecialchars');
32+
return $res;
33+
}
34+
public static function num_rows($query)
35+
{
36+
return $query->rowCount();
37+
}
38+
39+
public static function walk_recursive($obj, $closure)
40+
{
41+
if ( is_object($obj) )
42+
{
43+
$newObj = new stdClass();
44+
foreach ($obj as $property => $value)
45+
{
46+
$newProperty = $closure($property);
47+
$newValue = self::walk_recursive($value, $closure);
48+
$newObj->$newProperty = $newValue;
49+
}
50+
return $newObj;
51+
}
52+
elseif ( is_array($obj) )
53+
{
54+
$newArray = array();
55+
foreach ($obj as $key => $value)
56+
{
57+
$key = $closure($key);
58+
$newArray[$key] = self::walk_recursive($value, $closure);
59+
}
60+
return $newArray;
61+
}
62+
else
63+
{
64+
return $closure($obj);
65+
}
66+
}
67+
}

0 commit comments

Comments
 (0)