Coder Social home page Coder Social logo

php-jsondb's Introduction

php-jsondb

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

Usage

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

Initialize

	<?php 
	$json_db = new JSONDB();

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 only works as AND Operator at the moment

	<?php 
	$users = $json_db->select( 'name, state'  )
		->from( 'users.json' )
		->where( [ 'name' => 'Thomas' ] )
		->get();
	print_r( $users );
	
	$users = $json_db->select( 'name, state'  )
		->from( 'users.json' )
		->where( [ 'name' => 'Thomas', 'state' => 'Nigeria' ] )
		->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' ] )
		->get();
	

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

php-jsondb's People

Contributors

donjajo avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.