PHP code example of jajo / jsondb

1. Go to this page and download the library: Download jajo/jsondb library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jajo / jsondb example snippets


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


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


$users = $json_db->select( '*' )
	->from( 'users.json' )
	->get();
print_r( $users );

 
$users = $json_db->select( 'name, state'  )
	->from( 'users.json' )
	->get();
print_r( $users );
	

 
$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 );  	
	
	

$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. 

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

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


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


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

 
$json_db->to_mysql( 'users.json', 'users.sql', false );
 
if( $json_db->to_xml( 'users.json', 'users.xml' ) ) {
	echo 'Saved!';