Database using .json files for prototyping
- PHP 5.4+
- Prototypes/small projects
- When you want to store some data in JSON file instead of MySQL database.
Make sure you have following structure in your project:
bracketdb/
bracketdb/data
bracketdb/data/.htaccess
bracketdb/trash
bracketdb/Bracket.php
Add to working PHP file(s).
use Bracket\BracketDB as DB;
include('bracketdb/Bracket.php');
DB::create('users');
This is going to create users.json
in bracketdb/data
.
DB::table('users');
DB::trash('users');
Moves users.json
to bracketdb/trash
.
DB::restore('users');
If bracketsdb/trash/users.json
exists, it will be restored.
DB::delete('users');
This will permanently delete users.json
from file system.
Method | Info |
---|---|
all() | Returns whole table. |
get() | Gets the object. |
count() | Counts how many rows is in the object. |
first() | Gets the first object. |
max() | Returns highest column value. |
min() | Returns lowest column value. |
avg() | Returns average column value. |
sum() | Returns sum of all column. |
find() | Finds row by ID or specified column. |
lists() | Returns all values of specific column. |
where() | Condition (column, operator, value). |
andWhere() | Same as where(), but used as chain method. |
insert() | Inserts data (array) to table. |
insertAutoId() | Inserts data (array) with auto-assigned ID to table. |
orderBy() | Orders data ascending or descending. |
reorder() | Reorders records by column value and saves it. |
structure() | Returns structure of table. |
limit() | Sets limit to read methods. |
save() | Saves table. |
delete() | Deletes row. |
DB::table('users')->all();
Example (display all users with their ID and Name):
foreach(DB::table('users')->all() as $user) {
echo "<br>ID:".$user->id;
echo "<br>Name:".$user->name;
echo "<br>";
}
DB::table('users')->where($column, $operator, $value);
Example:
echo DB::table('users')->where('id', '=', '1')->first()->id;
first()
is required to get first and only row in this case.
Operator | Meaning |
---|---|
= | equal(s) |
== | equal(s) |
!= | not equal(s) |
<> | different than |
< | less than |
> | great than |
<= | less or equal than |
>= | great or equal than |
addWhere()
can be added to the end for extra condition.
DB::table('users')->orderBy($column, $type);
Example:
echo DB::table('users')->orderBy('name', 'asc')->get()[0]->name;
$arr = DB::table('users')->orderBy('name', 'asc')->get();
foreach($arr as $r) {
echo $r->name;
}
asc
for ascending order, ```desc' for descending.
Similiar to orderBy. It reorders all data and saves it.
DB::table('users')->reorder($column);
Example:
DB::table('users')->reorder('name');
$table = DB::table('users');
$table->where('id', '=', '1')->first()->name="New name";
$table->save();