PHP Classes

PHP MySQL JSON Manager: Build and Execute SQL queries with results in JSON

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 545 All time: 5,552 This week: 80Up
Version License PHP version Categories
mysql-json-manager 1.9GNU General Publi...5.2.9PHP 5, Databases
Description 

Author

This package can build and execute SQL queries with results in JSON.

It can compose SQL queries of several types, so the results are returned from the database already formatted in JSON format.

Currently it can extract specific parameters from GET or POST arrays, generate SQL expressions to return JSON formatted strings or object values, execute the queries to return the query results as a single JSON string.

Innovation Award
PHP Programming Innovation award nominee
February 2016
Number 2


Prize: One copy of the Zend Studio
Nowadays many applications retrieve data from databases and serve it in JSON format, so it can be easily processed by JavaScript applications like those based on AJAX.

This class can compose and execute queries so they already return JSON encoded results, thus without need to further encode them.

Manuel Lemos
Picture of Isaac Trenado Mx
  Performance   Level  
Innovation award
Innovation award
Nominee: 1x

 

Recommendations

Best Package to Address SQL Injection Vulnerabilities
Upgrading security of existing MySQL code

Example

<?php

/**
 * DropsizeMVCf - extension of the SlimFramework and others tools
 *
 * @author Isaac Trenado <isaac.trenado@codigolimpio.com>
 * @copyright 2013 Isaac Trenado
 * @link http://dropsize.codigolimpio.com
 * @license http://dropsize.codigolimpio.com/license.txt
 * @version 3.0.1
 * @package DropsizeMVCf
 *
 * DropsizeMVCf - Web publishing software
 * Copyright 2015 by the contributors
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * This program incorporates work covered by the following copyright and
 * permission notices:
 *
 * DropsizeMVCf is (c) 2013, 2015
 * Isaac Trenado - isaac.trenado@codigolimpio.com -
 * http://www.codigolimpio.com
 *
 * Wherever third party code has been used, credit has been given in the code's comments.
 *
 * DropsizeMVCf is released under the GPL
 *
 */
/**
 * Create Basic and complex examples Query to MYSQL
 *
 * @package com.dropsizemvcf
 * @author Isaac Trenado
 * @since 1.0.0
 */
include "./UnicodeUtf8Replace.php";
include
"./DPManagerJSON.php";
include
"./DPManager.php";
include
"./Conexion.php";

$con = new Conexion();

$con->setDebug(1);

/*
 * All samples are based on the "under Model" of Dropsize MVCF.
 * For more information check Dropsize MVCF
 */

/*
 *
 * array("ID" => "id_table",
 * "Metodo" => "POST",
 * "Tipo" => "None",
 * "Index" => array('id_cliente', 'nombre', 'paterno', 'materno'),
 * "Alias" => array(),
 * "Report" => array(),
 * "Params" => $args[0],
 * "Seguros" => $args[1]
 * )
 */

/*
 * Get Array From param GET or POST Request
 *
 * InPut :
 *
 * array("ID" => "id_table",
 * "Metodo" => "POST",
 * "Tipo" => "None",
 * "Index" => array('id_cliente', 'nombre', 'paterno', 'materno'),
 * "Alias" => array(),
 * "Report" => array(),
 * "Params" => $args[0],
 * "Seguros" => $args[1]
 * )
 *
 * URL : ?id_cliente=1&nombre=Super&paterno=Chinazo&materno=Poderoso&direccion=Mexico
 *
 * Output :
 *
 * Safe params
 *
 * Array(
 * [id_cliente] => 1
 * [nombre] => Super
 * [paterno] => Chinazo
 * [materno] => Poderoso
 * )
 *
 * Other params
 *
 * Array(
 *
 * [direccion] => Mexico
 * )
 *
 */

$model = array("ID" => "id_cliente"
   
, "Metodo" => "GET"
   
, "Tipo" => "Seguros"
   
, "Index" => array('id_cliente', 'nombre', 'paterno', 'materno')
    ,
"Alias" => array()
    ,
"Params" => array()
   
//, "Seguros" => array() // if not isset array GET
   
, "Seguros" => $_GET // if isset array GET or POST so uncomment this line
);

$campos = $lstUpdateQuery1 = DPManager::buildDatosTo($model, $otros);

echo
"<h1>Return array params required for the app and otros params no ables with app</h1>";
echo
"<pre>";
echo
"<h1>Safe params</h1>";
print_r($campos);
echo
"<h1>Other params</h1>";
print_r($otros);
echo
"</pre>";

/*
 *
 * DPManager::buildDatosJsonTo($model, "tabla", " nombre = 'Super' ")
 *
 * Output :
 *
 * CONCAT("{\"success\":true,\"total\":\"",(SELECT COUNT(*)
 * FROM tabla WHERE nombre = 'Super' ),"\",",
 * "\"rows\":[",GROUP_CONCAT(CONCAT("{"),CONCAT("\"id_cliente\":\"",id_cliente,"\","),
 * CONCAT("\"nombre\":\"",nombre,"\","),CONCAT("\"paterno\":\"",paterno,"\","),
 * CONCAT("\"materno\":\"",materno,"\""),CONCAT("}")), "]}") AS json
 *
 *
 * DPManager::buildSelectQuery($lstSimpleQuery, "tabla", " nombre = 'Super' ");
 *
 * Output :
 *
 * SELECT
 *
 * CONCAT("{\"success\":true,\"total\":\"",(SELECT COUNT(*) FROM tabla WHERE nombre = 'Super' ),
 * "\",", "\"rows\":[",GROUP_CONCAT(CONCAT("{"),CONCAT("\"id_cliente\":\"",id_cliente,"\","),
 * CONCAT("\"nombre\":\"",nombre,"\","),CONCAT("\"paterno\":\"",paterno,"\","),
 * CONCAT("\"materno\":\"",materno,"\""),CONCAT("}")), "]}") AS json
 *
 * FROM
 * tabla
 * WHERE
 * nombre = 'Super'
 *
 * Result :
 *
 * {"success":true,"total":"1","rows":[{"id_cliente":"1","nombre":"Super","paterno":"Chinazo","materno":"Poderoso"}]}
 *
 * NiceFormat : jsonlint.com
 *
 * {
 * "success": true,
 * "total": "1",
 * "rows": [{
 * "id_cliente": "1",
 * "nombre": "Super",
 * "paterno": "Chinazo",
 * "materno": "Poderoso"
 * }]
 * }
 *
 */

$lstSimpleFieldsQuery = DPManager::buildDatosJsonTo($model, "tabla", " nombre = 'Super' ");
$lstSelectJsonQry = DPManager::buildSelectQuery($lstSimpleFieldsQuery, "tabla", " nombre = 'Super' ");
$lstResult = DPManager::getField($con, $lstSelectJsonQry);

echo
"<h1>Return complex Query in format JSON</h1>";
echo
"<pre>";
echo
"<h3>Query format JSON</h3>";
print_r($lstSimpleFieldsQuery);
echo
"<h3>Query complete JSON Query</h3>";
print_r($lstSelectJsonQry);
echo
"<h2>Result</h2>";
print_r($lstResult);
echo
"</pre>";

/*
 *
 * DPManager::buildSingleJsonTo($parModelo, $pstExtra = false)
 *
 * Output :
 *
 * CONCAT("[",GROUP_CONCAT(CONCAT("{"),CONCAT("\"id_cliente\":\"",id_cliente,"\","),
 * CONCAT("\"nombre\":\"",nombre,"\","),CONCAT("\"paterno\":\"",paterno,"\","),
 * CONCAT("\"materno\":\"",materno,"\""),CONCAT("}")), "]") AS jsons
 *
 * DPManager::buildSelectQuery($lstSimpleSingleFieldsQuery, "tabla", false);
 *
 * Output :
 *
 * SELECT
 *
 * CONCAT("[",GROUP_CONCAT(CONCAT("{"),CONCAT("\"id_cliente\":\"",id_cliente,"\","),
 * CONCAT("\"nombre\":\"",nombre,"\","),CONCAT("\"paterno\":\"",paterno,"\","),
 * CONCAT("\"materno\":\"",materno,"\""),CONCAT("}")), "]") AS json
 *
 * FROM
 *
 * tabla
 *
 * Result :
 *
 * [{"id_cliente":"1","nombre":"Super","paterno":"Chinazo","materno":"Poderoso"},{"id_cliente":"2","nombre":"Will","paterno":"Smith","materno":"Ricardo"},{"id_cliente":"3","nombre":"Maicol","paterno":"Torres","materno":"Fuertes"}]
 *
 * NiceFormat : jsonlint.com
 *
 * [
 * {
 * "id_cliente": "1",
 * "nombre": "Super",
 * "paterno": "Chinazo",
 * "materno": "Poderoso"
 * },
 * {
 * "id_cliente": "2",
 * "nombre": "Will",
 * "paterno": "Smith",
 * "materno": "Ricardo"
 * },
 * {
 * "id_cliente": "3",
 * "nombre": "Maicol",
 * "paterno": "Torres",
 * "materno": "Fuertes"
 * }
 * ]
 */

$lstSimpleSingleFieldsQuery = DPManager::buildSingleJsonTo($model, false);
$lstSelectSingleJSON = DPManager::buildSelectQuery($lstSimpleSingleFieldsQuery, "tabla", false);
$lstJGetAllsJSON = DPManager::getField($con, $lstSelectSingleJSON);

echo
"<h1>Return complex Simple All Array Object in format JSON</h1>";
echo
"<pre>";
echo
"<h3>Query format JSON</h3>";
print_r($lstSimpleSingleFieldsQuery);
echo
"<h3>Query complete JSON Query</h3>";
print_r($lstSelectSingleJSON);
echo
"<h2>Result</h2>";
print_r($lstJGetAllsJSON);
echo
"</pre>";

/*
 *
 * DPManager::buildSingleJFieldsonTo($model)
 *
 * Output :
 *
 * CONCAT("[",GROUP_CONCAT(CONCAT("["),CONCAT("\"",id_cliente,"\""),
 * CONCAT("\"",nombre,"\""),CONCAT("\"",paterno,"\""),
 * CONCAT("\"",materno,"\""),CONCAT("]")), "]") AS json
 *
 * DPManager::buildSelectQuery($lstSimpleSingleFieldsQuery, "tabla", false);
 *
 * Output :
 *
 * SELECT
 *
 * CONCAT("[",GROUP_CONCAT(CONCAT("["),CONCAT("\"",id_cliente,"\""),
 * CONCAT("\"",nombre,"\""),CONCAT("\"",paterno,"\""),
 * CONCAT("\"",materno,"\""),CONCAT("]")), "]") AS json
 *
 * FROM
 *
 * tabla
 *
 * Result :
 *
 * [{"id_cliente":"1","nombre":"Super","paterno":"Chinazo","materno":"Poderoso"},{"id_cliente":"2","nombre":"Will","paterno":"Smith","materno":"Ricardo"},{"id_cliente":"3","nombre":"Maicol","paterno":"Torres","materno":"Fuertes"}]
 *
 * NiceFormat : jsonlint.com
 *
 * [
 * ["1", "Super", "Chinazo", "Poderoso"],
 * ["2", "Will", "Smith", "Ricardo"],
 * ["3", "Maicol", "Torres", "Fuertes"]
 * ]
 */

$lstJSONField = DPManager::buildSingleJFieldsonTo($model);
$lstSelectJSONFieldQuery = DPManager::buildSelectQuery($lstJSONField, "tabla");
$lstJSONFieldsAlls = DPManager::getField($con, $lstSelectJSONFieldQuery);

echo
"<h1>Return complex JSON Array Records</h1>";
echo
"<pre>";
echo
"<h3>Query format JSON</h3>";
print_r($lstJSONField);
echo
"<h3>Query complete JSON Query</h3>";
print_r($lstSelectJSONFieldQuery);
echo
"<h2>Result</h2>";
print_r($lstJSONFieldsAlls);
echo
"</pre>";

/*
 *
 * DPManager::buildSingleArrayFieldJsonTo($model)
 *
 * Output :
 *
 * CONCAT("[",GROUP_CONCAT(CONCAT("\"",id_cliente,"\","),
 * CONCAT("\"",nombre,"\","),CONCAT("\"",paterno,"\","),
 * CONCAT("\"",materno,"\"")), "]") AS json
 *
 * DPManager::buildSelectQuery($lstJSONArrayField, "tabla", "nombre = 'Super'");
 *
 * Output :
 *
 * SELECT
 *
 * CONCAT("[",GROUP_CONCAT(CONCAT("\"",id_cliente,"\","),
 * CONCAT("\"",nombre,"\","),CONCAT("\"",paterno,"\","),
 * CONCAT("\"",materno,"\"")), "]") AS json
 *
 * FROM
 *
 * tabla
 *
 * WHERE
 *
 * nombre = 'Super'
 *
 * Result :
 *
 * ["1","Super","Chinazo","Poderoso"]
 *
 * NiceFormat : jsonlint.com
 *
 * [
 * ["1", "Super", "Chinazo", "Poderoso"],
 * ["2", "Will", "Smith", "Ricardo"],
 * ["3", "Maicol", "Torres", "Fuertes"]
 * ]
 *
 */

$lstJSONArrayField = DPManager::buildSingleArrayFieldJsonTo($model);
$lstSelectJSONArrayQuery = DPManager::buildSelectQuery($lstJSONArrayField, "tabla", "nombre = 'Super'");
$lstJSONArrayRow = DPManager::getField($con, $lstSelectJSONArrayQuery);

echo
"<h1>Return Simple JSON Array Record</h1>";
echo
"<pre>";
echo
"<h3>Query format JSON</h3>";
print_r($lstJSONArrayField);
echo
"<h3>Query complete JSON Query</h3>";
print_r($lstSelectJSONArrayQuery);
echo
"<h2>Result</h2>";
print_r($lstJSONArrayRow);
echo
"</pre>";

/*
 *
 * DPManager::setSessionGroupConcatMaxLen($con);
 *
 * Output :
 *
 * ADORecordSet_empty Object
 * (
 * [dataProvider] => empty
 * [databaseType] =>
 * [EOF] => 1
 * [_numOfRows] => 0
 * [fields] =>
 * [connection] =>
 * )
 *
 * Result :
 *
 * SET SESSION group_concat_max_len = 4294967295
 *
 *
 */

$lobResult = DPManager::setSessionGroupConcatMaxLen($con);

echo
"<h1>Increase the limit from GroupConcat Max Length</h1>";
echo
"<pre>";
echo
"<h3>Query to increase the limit from function Group_concat()</h3>";
print_r($lobResult);



Details

DPManagerJSON

BuildQuerys From Arrays to get Results in format JSON, Array, Array object, Objects. Crossdomain


  Files folder image Files (168)  
File Role Description
Files folder imageadodb (26 files, 9 directories)
Accessible without login Plain text file basic.php Example Example script
Plain text file Conexion.php Class Class source
Plain text file DPManager.php Class Class source
Plain text file DPManagerJSON.php Class Class source
Accessible without login Plain text file dropsize_sql.sql Data Auxiliary data
Accessible without login HTML file example.html Data Example script
Accessible without login Plain text file README.md Data Auxiliary data
Plain text file UnicodeUtf8Replace.php Class Class source

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
 Version Control Unique User Downloads Download Rankings  
 100%
Total:545
This week:0
All time:5,552
This week:80Up
User Comments (1)
peace and love, obviously that guy looks like he doesn´t was...
9 years ago (Marco Antonio Ramos Valencia)
80%StarStarStarStarStar