|
| 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 |
0 commit comments