2017 © Pedro Peláez
 

library php-jsondb

Simple JSON-file-database PHP library

image

mrkody/php-jsondb

Simple JSON-file-database PHP library

  • Tuesday, February 27, 2018
  • by mrkody
  • Repository
  • 1 Watchers
  • 0 Stars
  • 6 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 8 Forks
  • 1 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

php-jsondb

A PHP Class that reads JSON file as a database. Use for sample DBs., (*1)

Usage

Include the file <?php include( 'JSONDB.Class.php' );?>, (*2)

Initialize

    <?php 
    $json_db = new JSONDB();

Inserting

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

Get back data, just like MySQL in PHP, (*5)

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, (*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 );      


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, (*7)

    <?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, (*8)

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

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

Deleting Row

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

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

Exporting to MySQL

You can export the JSON back to SQL file by using this method and providing an output, (*11)

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

Disable CREATE TABLE, (*12)

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

Exporting to XML

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!'; }, (*13)

The Versions

27/02 2018

dev-master

9999999-dev https://github.com/mrkody/php-jsondb

Simple JSON-file-database PHP library

  Sources   Download

MIT

The Requires

  • php >=5.3.0
  • ext-json *

 

by Avatar mrkody
by Avatar donjajo

php json jsondb mrkody