dev-master
9999999-devA PHP Class that reads JSON file as a database. Use for sample DBs
MIT
The Development Requires
by Jajo
A PHP Class that reads JSON file as a database. Use for sample DBs
A PHP Class that reads JSON file as a database. Use for sample DBs., (*1)
Install package composer require jajo/jsondb
, (*2)
<?php use Jajo\JSONDB; $json_db = new JSONDB( __DIR__ ); // Or passing the directory of your json files with no trailing slash, default is the current directory. E.g. new JSONDB( '/var/www/html/json_files' )
Insert into your new JSON file. Using users.json as example here, (*3)
NB: Columns inserted first will be the only allowed column on other inserts, (*4)
<?php $json_db->insert( 'users.json', [ 'name' => 'Thomas', 'state' => 'Nigeria', 'age' => 22 ] );
Get back data, just like MySQL in PHP, (*5)
<?php $users = $json_db->select( '*' ) ->from( 'users.json' ) ->get(); print_r( $users );
<?php $users = $json_db->select( 'name, state' ) ->from( 'users.json' ) ->get(); print_r( $users );
This WHERE works as AND Operator at the moment or OR, (*6)
<?php $users = $json_db->select( 'name, state' ) ->from( 'users.json' ) ->where( [ 'name' => 'Thomas' ] ) ->get(); print_r( $users ); // Defaults to Thomas OR Nigeria $users = $json_db->select( 'name, state' ) ->from( 'users.json' ) ->where( [ 'name' => 'Thomas', 'state' => 'Nigeria' ] ) ->get(); print_r( $users ); // Now is THOMAS AND Nigeria $users = $json_db->select( 'name, state' ) ->from( 'users.json' ) ->where( [ 'name' => 'Thomas', 'state' => 'Nigeria' ], 'AND' ) ->get(); print_r( $users );
By passingJSONDB::regex
to where statement, you can apply regex searching. It can be used for implementing LIKE
or REGEXP_LIKE
clause in SQL., (*7)
$users = $json_db->select( 'name, state' ) ->from( "users" ) ->where( array( "state" => JSONDB::regex( "/ria/" )), JSONDB::AND ) ->get(); print_r( $users ); // Outputs are rows which contains "ria" string in "state" column.
Thanks to Tarun Shanker for this feature. By passing the order_by()
method, the result is sorted with 2 arguments of the column name and sort method - JSONDB::ASC
and JSONDB::DESC
, (*8)
<?php $users = $json_db->select( 'name, state' ) ->from( 'users.json' ) ->where( [ 'name' => 'Thomas' ] ) ->order_by( 'age', JSONDB::ASC ) ->get(); print_r( $users );
You can also update same JSON file with these methods, (*9)
<?php $json_db->update( [ 'name' => 'Oji', 'age' => 10 ] ) ->from( 'users.json' ) ->where( [ 'name' => 'Thomas' ] ) ->trigger();
Without the where() method, it will update all rows, (*10)
<?php $json_db->delete() ->from( 'users.json' ) ->where( [ 'name' => 'Thomas' ] ) ->trigger();
Without the where() method, it will deletes all rows, (*11)
You can export the JSON back to SQL file by using this method and providing an output, (*12)
<?php $json_db->to_mysql( 'users.json', 'users.sql' );
Disable CREATE TABLE, (*13)
<?php $json_db->to_mysql( 'users.json', 'users.sql', false );
Tarun Shanker also provided a feature to export data to an XML file ```php <?php if( $json_db->to_xml( 'users.json', 'users.xml' ) ) { echo 'Saved!'; }, (*14)
A PHP Class that reads JSON file as a database. Use for sample DBs
MIT