PHP JSON file as Database
Hello Everyone, I wanna show you some of my projects, I was tried to use to save some data on server PHP without to install a Database or SQL,
So built for some classes with simple write JSON files and works like NoSQL, So let’s see how it works?
We need to Select our JSON File in Config/DBJsonCon.php
<?php
// We connect the config class file
$docRoot = $_SERVER['DOCUMENT_ROOT'];
$DBJsonClass = $docRoot . '/config/DBJsonClass.php';
require_once $DBJsonClass;
$DBJ = new DBJ();
// We get the Data from our JSON File
$dbTableJson = $docRoot . '/DB/db.json';
$DBJ->jsonFileLocation = $dbTableJson;
?>
Add this lines for Start to work
$docRoot = $_SERVER['DOCUMENT_ROOT'];
$DBJsonCon = $docRoot . '/config/DBJsonCon.php';
require_once $DBJsonCon;
Get
<?php
// We get the DBJsonCon.php file
$docRoot = $_SERVER['DOCUMENT_ROOT'];
$DBJsonCon = $docRoot . '/config/DBJsonCon.php';
require_once $DBJsonCon;
// Select By Column
// $QGet = $DBJ->getLine(Value,Column,Group);
$QGet = $DBJ->getLine("[email protected]","Email","Customers");
if($QGet != 0){
print_r($QGet);
}
?>
Add
<?php
// We get the DBJsonCon.php file
$docRoot = $_SERVER['DOCUMENT_ROOT'];
$DBJsonCon = $docRoot . '/config/DBJsonCon.php';
require_once $DBJsonCon;
// Insert Query
$q = $DBJ->addLine('
{
"Name" : "Edi Cohen",
"Email": "[email protected]",
"City" : "Tel Aviv"
}
',"Customers");
echo $q;
?>
Update
<?php
// We get the DBJsonCon.php file
$docRoot = $_SERVER['DOCUMENT_ROOT'];
$DBJsonCon = $docRoot . '/config/DBJsonCon.php';
require_once $DBJsonCon;
// Update By Column
// $QUpdate = $DBJ->update(Value,Column,NewValue,Group);
$QUpdate = $DBJ->update("[email protected]","Email","[email protected]","Customers");
echo $QUpdate;
?>
Remove
<?php
// We get the DBJsonCon.php file
$docRoot = $_SERVER['DOCUMENT_ROOT'];
$DBJsonCon = $docRoot . '/config/DBJsonCon.php';
require_once $DBJsonCon;
// Delete By Column
// $QRemove = $DBJ->removeLine(Value,Column,Group);
$QRemove = $DBJ->removeLine("[email protected]","Email","Customers");
echo $QRemove;
?>
All the files is here :
https://github.com/neryaelul/Database-JSON-PHP