PHP Classes

How to Create and Query a PHP JSON Database Using the Package jsqldb: Examples to use JSQLDB to store data in JSON files

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2025-04-23 (6 months ago) RSS 2.0 feedNot yet rated by the usersTotal: Not yet counted Not yet ranked
Version License PHP version Categories
jsqldb 0.0.1Custom (specified...8.2Databases, Files and Folders, PHP 8
Description 

Author

This package provides examples to use JSQLDB to store data in JSON files.

It has example scripts that can use Ascoos framework JSQLDB package to show how to create and query a database using files that contain data in the JSON format.

Currently it provides examples to:

- Create a database

- Create a database user

- Insert new table records

- Query table records

Picture of ASCOOS CMS
  Performance   Level  
Name: ASCOOS CMS <contact>
Classes: 28 packages by
Country: Greece Greece
Age: ???
All time rank: 379423 in Greece Greece
Week rank: 5 Up1 in Greece Greece Up
Innovation award
Innovation award
Nominee: 16x

Winner: 1x

Instructions

Please read this document to learn how to configure, create and query a database stored in JSON files.

Example

<?php
use ASCOOS\FRAMEWORK\Kernel\DB\JSQLDB;

// We read from the settings file the operating parameters of the database.
$conf = require "../conf/config.php";

$properties['tables_prefix'] = 'ascoos'; // It will give e.g. a "ascoos_articles' table.

// Initialize the database object
$jsql = new TJSQLDB($conf, $properties);

// Create Database
$jsql->createDatabase('test_db');

// Create User and assign to Database
$jsql->createUser('admin', 'root', 'test_db');

// Select Current Database
$jsql->select_db('test_db');

// Create Table
$schema = [
 
'id' => 'INT NOT NULL AUTO_INCREMENT PRIMARY KEY',
 
'article_id' => 'INT UNSIGNED NOT NULL DEFAULT 0',
 
'cat_id' => 'INT UNSIGNED NOT NULL DEFAULT 0',
 
'user_id' => 'INT UNSIGNED NOT NULL DEFAULT 0',
 
'lang_id' => 'INT UNSIGNED NOT NULL DEFAULT 0',
 
'title' => 'VARCHAR(200) NOT NULL',
 
'content' => ' TEXT NULL COMPRESSED',
 
'created' => ' DATETIME NULL DEFAULT CURRENT_TIMESTAMP()',
 
'updated' => 'DATETIME NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP()'
];
$jsql->createTable('#__articles', $schema);

// Insert Data
$query->setSQLQuery("INSERT INTO #__articles (article_id, cat_id, user_id, lang_id, title, content) VALUES
(1, 1, 1, 1, 'Title 1', 'Test Content 1'),
(1, 1, 1, 2, 'Title 2', 'Test Content 2'),
(2, 2, 1, 1, 'Title 3', 'Test Content 3'),
(3, 3, 1, 1, 'Title 4', 'Test Content 4'),
(3, 3, 1, 2, 'Title 5', 'Test Content 5');
"
);
$query->execute();

$query = "SELECT article_id AS aid, title, content AS doc FROM #__articles WHERE user_id = ".$my->id." AND lang_id = 1 ORDER BY created DESC LIMIT 10";
$jsql->setSQLQuery($query);
$jsql->execute();
$data = $jsql->getResults();

// Close all open database resources
$jsql->close();

print_r($data);
?>


Details

JSQLDB - JSON SQL Database for PHP

? A lightweight, SQL-like JSON-based database for PHP

JSQLDB is a flexible database system that leverages JSON for storage and provides SQL-like queries without requiring SQLite or MySQL. It?s lightweight, fast, and perfect for applications that need portability and security.

JSQL Database

? Features

  • ? JSON-based storage without DLL/SO dependencies
  • ? SQL-like queries with support for `SELECT`, `INSERT`, `UPDATE`, `DELETE`, `JOIN`, `UNION`, `GROUP BY`, `HAVING`, `LIMIT`, `ORDER BY`, `DISTINCT` etc.
  • ? Indexing support for fast searching
  • ? Compressed storage for optimized resource usage
  • ? Customizable storage path for secure data management
  • ? Optimized for PHP 8.2+

? Requires:

  • PHP 8.2+
  • Ascoos Framework (For high level access, and managed)
  • ionCube Loaders

? Installation

git clone https://github.com/alexsoft-software/jsql.git
cd jsql
composer install

?? More details coming soon in the documentation!

? Using the Database

The database must be initialized before using it. This is done through the settings file.

? Example Settings


return [
    'jsql' => [
        'config_path' => '/root/path/conf/config.json', 
        'users_path' => '/root/path/conf/users.json',
        'databases_root_path' => '/root/path/jsql_db',      
    ]
];

? Example Usage

<?php
use ASCOOS\FRAMEWORK\Kernel\DB\JSQLDB;

// We read from the settings file the operating parameters of the database.
$conf = require "conf/config.php";

$properties['tables_prefix'] = 'ascoos'; // It will give e.g. a "ascoos_articles' table.

// Initialize the database object
$jsql = new TJSQLDB($conf, $properties);

// Create Database
$jsql->createDatabase('test_db');

// Create User and assign to Database
$jsql->createUser('admin', 'root', 'test_db');

// Select Current Database
$jsql->select_db('test_db');

// Create Table
$sql = "CREATE TABLE `#__articles` (
  `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `article_id` INT UNSIGNED NOT NULL DEFAULT 0,
  `cat_id` INT UNSIGNED NOT NULL DEFAULT 0,
  `user_id` INT UNSIGNED NOT NULL DEFAULT 0,
  `lang_id` INT UNSIGNED NOT NULL DEFAULT 0,
  `title` VARCHAR(200) NOT NULL,
  `content` TEXT NULL COMPRESSED,
  `created` DATETIME NULL DEFAULT CURRENT_TIMESTAMP(),
  `updated` DATETIME NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP()
);";

$jsql->setSQLQuery($sql);
$jsql->execute();

// Insert Data
$query->setSQLQuery("INSERT INTO #__articles (article_id, cat_id, user_id, lang_id, title, content) VALUES 
(1, 1, 1, 1, 'Title 1', 'Test Content 1'),
(1, 1, 1, 2, 'Title 2', 'Test Content 2'),
(2, 2, 1, 1, 'Title 3', 'Test Content 3'),
(3, 3, 1, 1, 'Title 4', 'Test Content 4'),
(3, 3, 1, 2, 'Title 5', 'Test Content 5');
");
$query->execute();

$query = "SELECT article_id AS aid, title, content AS doc FROM #__articles WHERE user_id = ".$my->id." AND lang_id = 1 ORDER BY created DESC LIMIT 10";
$jsql->setSQLQuery($query);
$jsql->execute();
$data = $jsql->getResults();

// Close all open database resources
$jsql->close();

print_r($data);
?>

? Alternative way to create a table


// Direct way to create a table and its structure.
$schema = [
  'id' => 'INT NOT NULL AUTO_INCREMENT PRIMARY KEY',
  'article_id' => 'INT UNSIGNED NOT NULL DEFAULT 0',
  'cat_id' => 'INT UNSIGNED NOT NULL DEFAULT 0',
  'user_id' => 'INT UNSIGNED NOT NULL DEFAULT 0',
  'lang_id' => 'INT UNSIGNED NOT NULL DEFAULT 0',
  'title' => 'VARCHAR(200) NOT NULL',
  'content' => ' TEXT NULL COMPRESSED',
  'created' => ' DATETIME NULL DEFAULT CURRENT_TIMESTAMP()',
  'updated' => 'DATETIME NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP()'
];

$jsql->createTable('#__articles', $schema);

?? See more examples on the official website!


  Files folder image Files (6)  
File Role Description
Files folder imageconf (1 file)
Files folder imageexamples (2 files)
Accessible without login Plain text file LICENSE_AGL-F.md Lic. License text
Accessible without login Plain text file README-GR.md Doc. Greek Readme
Accessible without login Plain text file README.md Doc. Readme

  Files folder image Files (6)  /  conf  
File Role Description
  Accessible without login Plain text file config.php Aux. Configurtion

  Files folder image Files (6)  /  examples  
File Role Description
  Accessible without login Plain text file example1.php Example Example script
  Accessible without login Plain text file example2.php Example Example script

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
Downloadjsqldb-2025-04-23.zip
Downloadjsqldb-2025-04-23.tar.gz
Install with ComposerInstall with Composer
Needed packages  
Class DownloadWhy it is needed Dependency
Ascoos Framework Download .zip .tar.gz Uses classes of the Ascoos framework Required
Ascoos OS Download .zip .tar.gz The version 26 is based on Ascoos OS Required
 Version Control Unique User Downloads  
 100%
Total:0
This week:0