This is a simple JSON database coded with PHP. I also included a test page to demonstrate all the database calls.
- Drop src files into a directory
- Open test.php and test database functions
- To make a new JSON DB file, create a new .json file and add curly braces {} with a code editor
- You can now use that file name in your database calls
Sets database row and properties. Returns the new row ID string.
- fileName: name of JSON file to write row too
- dataArray: key/value array of properties to be added
- status: Boolean (true/false) - True = successful. False = error
- data: String - Row ID if successful
$propsArray = array();
$propsArray['name'] = "Jason";
$propsArray['message'] = "Hello World";
$result = setRow("testFile", $propsArray);
if ($result['status']) {
echo $result['data'] . "<br><br>";
}
Updates a database row. This is JSON so you can also add new props. Returns row ID string.
- fileName: name of JSON file to write row too
- rowID: id of row to update
- props: key/value array of properties to be added
- status: Boolean (true/false) - True = successful. False = error
- data: String - Row ID if successful
$propsArray = array();
$propsArray['name'] = "Jason & Jax";
$propsArray['message'] = "Hello Again World";
$result = updateRow("testFile", "6518f521c7631", $propsArray);
if ($result['status']) {
echo $result['data']['id'] . "<br><br>";
}
Returns all rows in the JSON file. Result data is in array of arrays.
- fileName: name of JSON file to write row too
- status: Boolean (true/false) - True = successful. False = error
- data: Array of Arrays - All row data in key/value arrays if successful
$result = getAllRows("testFile");
if ($result['status']) {
foreach ($result['data'] as $key=>$row) {
echo $key . "<br>";
echo $row['name'] . "<br>";
echo $row['message'] . "<br><br>";
}
}
Returns row by ID. Result data is a single array.
- fileName: name of JSON file to write row too
- rowID: id of row to update
- status: Boolean (true/false) - True = successful. False = error
- data: Array - Row data in key/value array if successful
$result = getRow("testFile", "6518f521c7631");
if ($result['status']) {
echo $result['data']['id'] . "<br>";
echo $result['data']['name'] . "<br>";
echo $result['data']['message'] . "<br><br>";
}
Returns row by property name/value. Result data is a single array.
- fileName: name of JSON file to write row too
- propName: key name of property to query for
- propName: value of property to query for
- status: Boolean (true/false) - True = successful. False = error
- data: Array - Row data in key/value array if successful
$result = queryDB("testFile","name","jason");
if ($result['status']) {
foreach ($result['data'] as $key=>$row) {
echo $key . "<br>";
echo $row['name'] . "<br>";
echo $row['message'] . "<br><br>";
}
}
Deletes row by ID. Only returns a restult status.
- fileName: name of JSON file to write row too
- rowID: id of row to delete
- propName: value of property to query for
- status: Boolean (true/false) - True = successful. False = error
- data: empty
$result = deleteRow("testFile","6518f521c7631");
if ($result['status']) {
echo "Row Deleted<br><br>";
}