GitHub - donjajo/php-jsondb: A PHP Class that reads JSON file as a database. Use for sample DBs

php-jsondb

A PHP Class that reads JSON file as a database. Use for sample DBs.

Usage

Install package composer require jajo/jsondb

Initialize

<?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' )

Inserting

Insert into your new JSON file. Using users.json as example here

NB: Columns inserted first will be the only allowed column on other inserts

<?php
$json_db->insert( 'users.json', 
	[ 
		'name' => 'Thomas', 
		'state' => 'Nigeria', 
		'age' => 22 
	]
);

Get

Get back data, just like MySQL in PHP

All columns:
<?php
$users = $json_db->select( '*' )
	->from( 'users.json' )
	->get();
print_r( $users );
Custom Columns:
<?php 
$users = $json_db->select( 'name, state'  )
	->from( 'users.json' )
	->get();
print_r( $users );
	
Where Statement:

This WHERE works as AND Operator at the moment or OR

<?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 );  	
	
	
Order By:

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[1]

<?php 
$users = $json_db->select( 'name, state'  )
	->from( 'users.json' )
	->where( [ 'name' => 'Thomas' ] )
	->order_by( 'age', JSONDB::ASC )
	->get();
print_r( $users );

Updating Row

You can also update same JSON file with these methods

<?php 
$json_db->update( [ 'name' => 'Oji', 'age' => 10 ] )
	->from( 'users.json' )
	->where( [ 'name' => 'Thomas' ] )
	->trigger();
	

Without the where() method, it will update all rows

Deleting Row

<?php
$json_db->delete()
	->from( 'users.json' )
	->where( [ 'name' => 'Thomas' ] )
	->trigger();

Without the where() method, it will deletes all rows

Exporting to MySQL

You can export the JSON back to SQL file by using this method and providing an output

<?php 
$json_db->to_mysql( 'users.json', 'users.sql' );

Disable CREATE TABLE

<?php 
$json_db->to_mysql( 'users.json', 'users.sql', false );

Exporting to XML

Tarun Shanker[2] also provided a feature to export data to an XML file

<?php 
if( $json_db->to_xml( 'users.json', 'users.xml' ) ) {
	echo 'Saved!';
}

References

  1. ^ Tarun Shanker (in.linkedin.com)
  2. ^ Tarun Shanker (in.linkedin.com)
keywords

No Items Found.

Add Comment
Type in a Nick Name here
 
Search Linx
Search Linx by entering your search text above.
Welcome

This is my test area for webdev. I keep a collection of code here, mostly for my reference. Also if i find a good link, i usually add it here and then forget about it. more...

Subscribe to weekly updates about things i have added to the site or thought interesting during the last week.

You could also follow me on twitter or not... does anyone even use twitter anymore?

If you found something useful or like my work, you can buy me a coffee here. Mmm Coffee. ☕

❤️👩‍💻🎮

🪦 2000 - 16 Oct 2022 - Boots
Random Quote
In this case my anchor this week becomes driving almost 2hrs outside of Atlanta to one of my favorite hard core gyms in the world.. MetroFlex aka The Dungeon. The gym owners turn the heat way up so it becomes a fun sweat box and the gym members just watch from afar and leave me alone. I happily drive myself long distances to find MY ANCHOR. Our anchor allows us to have balance, focus and be as productive as possible. And if you're in the middle of a heavy set and your headphones start to fall off your head, like mine did here.. well.. f*ck the headphones. Let em break and fall. You can always get a new pair, but the iron ain't ever gonna lift itself.
Unknown
Random CSS Property

<custom-ident>

The <custom-ident> CSS data type denotes an arbitrary user-defined string used as an identifier. It is case-sensitive, and certain values are forbidden in various contexts to prevent ambiguity.
<custom-ident> css reference