JsonDb is a PHP class used to CRUD (INSERT
, UPDATE
, SELECT
, DELETE
) json file like SQL.
It's easy and lightweight.
- Create a directory
files
and a demo file namedemo.json
located the directory. - Include JsonDb.php in your project,
new JsonDb
and identify json files directory:include 'JsonDb.php'; $json = new JsonDb('./files');
- Then you can insert/select a record to
demo.json
:$data = ['name' => 'Gary', 'title' => 'PHP', 'website' => 'http://www.awaimai.com/']; echo $json->demo->insert($data); //return the inserted id print_r($json->demo->selectAll()); // return all record $json->demo->delete('*');
Here, property demo
of $json
is same as the name of json file demo.json
(exclude extension .json
).
Create JsonDB with $directory='./files'
, $primary='page_id'
, and file $extension='.js'
:
$json = new JsonDb('./files', 'page_id', '.js');
Note: directory is required, primary key default is id
, file extension default is '.json'
.
$json->demo->select(2); // Select a record which's primary key is 2
$json->demo->selectAll(); // Select all records
$json->demo->selectAll(['sort' => 'DESC'); // Select all records ordered desc by sort
$json->demo->selectIn([2, 3, 5]); // Select some records
$json->demo->count(); // Number of records
$json->demo->insert(['name' => 'Gary', 'title' => 'PHP']);
$json->demo->update(['id' => 1, 'name' => 'Galley']);
This will change the name of a record which's id is 1
, other field will not be affected.
$data = [
['id' => 1, 'name' => 'Jack'],
['id' => 3, 'title' => 'Javascript'],
];
$json->demo->updates($data);
You must identify primary key value in the array.
$json->demo->delete(2); // Delete one record
$json->demo->delete([2, 3, 5]); // Delete some records
$json->demo->delete('*'); // Delete *all* records