PHP Classes

PHP Restful Database Query: Perform MySQL queries and return JSON responses

Recommend this page to a friend!
  Info   View files Example   View files View files (10)   DownloadInstall with Composer Download .zip   Reputation   Support forum (2)   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStar 46%Total: 403 All time: 6,536 This week: 555Up
Version License PHP version Categories
simplify-query 1.0.11The PHP License7Databases, PHP 7
Description 

Author

This package can perform MySQL queries and return JSON responses.

It can perform several types of operations with table records in a MySQL database.

Currently it can retrieve, insert, update, delete individual records, update multiple records, get multiple records, verify if a given table records exist, insert multiple records, retrieve a limited number of records,count records and retrieve the last record by index field.

The response is an object formatted as a JSON string.

Picture of Kazeem Olanipekun
  Performance   Level  
Name: Kazeem Olanipekun <contact>
Classes: 1 package by
Country: Nigeria Nigeria
Age: 34
All time rank: 353426 in Nigeria Nigeria
Week rank: 4277 Down42 in Nigeria Nigeria Down

Recommendations

How to find package ?
I'd like find package for accessing MySQL database.

Access MySQL database from RESTful Web Service
I need to return data from a MySQL database using REST

Example

<?php
/**
 * Created by PhpStorm.
 * User: SQ05
 * Date: 08/02/2017
 * Time: 11:43 PM
 */
require_once ('../interface/BeereOperations.php'); //load dependencies
echo"
/***
 * This 'Beere' Class is of PHP 7, other lower version might have problem to use the query methods.<br>
 * This 'Beere' class reduces many mysqli query you need to do on every service request<br>
 * this 'Beere' class helps to structure all operations of your app in one file.<br>
 * it make use of array of key and value of which the key correspond to table field and value is content to save.<br>
 * request are sent in array form and response is send in Json to the caller.<br>
 * There are many operations this class of BeereOperations can perform to help develop a restful response service synchronously and asynchronously.<br>
 * To save nested array of level 2, use saveMultiples.<br>
 * The response returns json of five category of data, which are:<br>
 * Status which is Integer to denote 200 for success and 400 for failure.<br>
 * Message which is string, <br>
 * Data which can either be json of data queried<br>
 * Code is bool to denote success<br>
 * Total: this is majorly used while using list based on limit. <br>
 * We will cover a limited execution of queries with CRUD using our class.<br>
 * Kindly change the connection in utils folder to connect with your own database and create a table user with email, first_name, * status fields.<br>
 * <br><b>Study it well to see how powerful it is by using one or more together to achieve your goal. it is well optimised.</b>
 */
"
."<br>";
echo
"<br>";echo "<br>";echo "<br>";
$_POST=array('first_name'=>'Kazeem Olanipekun','email'=>'wally@gmail.com','status'=>'Mr','middle_name'=>"value'); DROP TABLE table;--"); //Assuming this is a post request

function saveUser(){
    echo
"<br>";
    echo
"<b>Saving with Beere Class Asynchronously</b>"."<br>";
$beere= new BeereOperations();
 
$save=$beere->save('user', $_POST); //request with asynchronous response without the saved id;
   
echo"<b>Asynchronous save of data</b>"."<br>";echo "<br>";

  
print_r(json_decode($save,true)); //convert json back to array
   
echo "<br>";echo "<br>";

 
$_POST['key_fetch']=md5(mt_rand(0,999).date('YmdHis',time()).uniqid('key').mt_rand(0,99999));
   
//load syncronously with key_fetch
   
echo"<b>always create key_fetch of the same type and pass to each row of nested array or to an array to receive response
   synchronously.</b>"
."<br>";echo "<br>";
    echo
"<b>Saving with Beere Class Synchronously</b>"."<br>";echo "<br>";
   
$save=$beere->save('user', $_POST);
  
$_GET['user']=json_decode($save,true);
      
print_r($_GET['user']); //convert json back to array
   
echo "<br>";echo "<br>";
}
function
updateUser(){
    echo
"<b>Update Data using parameters</b>"."<br>";echo "<br>";echo "<br>";
   
$delete= new BeereOperations();
   
$where=array('id'=>1);
   
$sets=array('first_name'=>'Fola Olawale','email'=>'kezyolanipekun@gmail.com','status'=>'Mr');
   
$result= $delete->update('user', $where, $sets);
   
//You pass a single conditional operator such as AND etc as 3rd parameter in update method, default is AND
   
print_r(json_decode($result,true));
}
function
listUser(){
    echo
"<br>";echo "<br>";echo "<br>";
    echo
"<b>Listing respond with list of arrays</b>"."<br>";
    echo
"List Data using empty data array"."<br>";
$list=new BeereOperations();
 
//to list all, pass empty array, while to list based on some data, pass array of data.
   
$data=array();
   
//$res=$list->list('user', $data);
   // print_r(json_decode($res,true));
    //You pass a single conditional operator such as AND etc as 3rd parameter in update method, default is AND
   
echo "<br>";echo "<br>";
    echo
"<b>List Data using Parameter(s)</b>"."<br>";echo "<br>";
   
$data=array('1d'=>1);
   
$res=$list->list('user', $data);
   
print_r(json_decode($res,true));
}
function
validateUser(){
    echo
"<br>";echo "<br>";echo "<br>";
    echo
"<b>Validate User by Parameter</b>"."<br>";echo "<br>";
   
$val=new BeereOperations();
   
$data=array('email'=>'kezyolanipekun@gmail.com');
   
$res=$val->validate('user', $data);
   
print_r(json_decode($res,true)); //convert json back to array
   
echo "<br>";echo "<br>";
}

function
getData(){
    echo
"<br>";echo "<br>";echo "<br>";
    echo
"<b>A get respond with an array unlike list with multiple arrays"."<br>";
    echo
"Get Data using empty data array will throw error, cause daya must not be empty</b>"."<br>";
   
$get=new BeereOperations();
   
//to list all, pass empty array, while to list based on some data, pass array of data.
   
$data=array('id'=>25); //data must not be empty array
   
$res=$get->getADataByParam('user', $data);
   
print_r(json_decode($res,true));
   
//You pass a single conditional operator such as AND etc as 3rd parameter in getADataByParam method, default is AND
   
echo "<br>";echo "<br>";
    echo
"<b>Get Data using Parameter(s) return a single array of data</b>"."<br>";
   
$data=array('id'=>1);
   
$res=$get->getADataByParam('user', $data);
   
print_r(json_decode($res,true));
    echo
"<br>";
}
function
saveUsingMultiple(){
    echo
"<br>";echo "<br>";
  
$data=array(
       array(
'first_name'=>'Fola Olawale','email'=>'dennis@gmail.com','status'=>'Mr'),
       array(
'first_name'=>'Kunle Olawale','email'=>'d@gmail.com','status'=>'Mr'),
       array(
'first_name'=>'Kunle Loveth','email'=>'love@gmail.com','status'=>'Miss')
   );

    echo
"<b>Saving using MULTIPLE Data with Beere Class Asynchronously</b>"."<br>";
   
$beere= new BeereOperations();
   
$save=$beere->saveMultiple('user', $data); //request with asynchronous response without the saved id;
   
echo"Asynchronous save of data"."<br>";echo "<br>";

   
print_r(json_decode($save,true)); //convert json back to array
   
echo "<br>";echo "<br>";

    echo
"<b>Saving using MULTIPLE Data with Beere Class Synchronously by adding key_fetch to request</b>"."<br>";

   
$key_fetch=md5(mt_rand(0,999).date('YmdHis',time()).uniqid('key').mt_rand(0,99999));

    foreach (
$data as $index => &$item) {
       
$item['key_fetch']=$key_fetch;
    };

   
//load syncronously with key_fetch
   
echo"<b>always create key_fetch of the same type and pass to each row of nested array or to an array to receive response
   synchronously.</b>"
."<br>";echo "<br>";

   
$save=$beere->saveMultiple('user', $data);
   
$_GET['userMultiple']=json_decode($save,true);
   
print_r($_GET['userMultiple']); //convert json back to array
   
echo "<br>";echo "<br>";
}
function
listByLimitPager(){
    echo
"<br>";echo "<br>";echo "<br>";
    echo
"<b>List By Limit Pager</b>"."<br>";
   
$val=new BeereOperations();
   
//pass parameter or not, it is work
   
$page=1; //default is one but make sure you increment page and send d next page to fetch the next data.
   
$limit=10; //default is 200;
   
$logic='&&'; //default is AND.
   
while($page<3) {
        echo
" List ".$page.'<br>';
       
$res=$val->listByLimit('user', array(), $logic, $page, $limit,['first_name']);
       
print_r(json_decode($res, true)); //convert json back to array
       
echo "<br><br>";
       
$page++;
    }
    echo
"<br>";
}
function
deleteUser(){
    echo
"<br>";echo "<br>";echo "<br>";
    echo
"<b>Delete Data using parameters</b>"."<br>";echo "<br>";
   
$delete= new BeereOperations();
   
$data=array('id'=>2);
   
$result= $delete->delete('user', $data);
   
//You pass a single conditional operator such as AND etc as 3rd parameter in delete method, default is AND
   
print_r(json_decode($result,true));
    echo
"<br>";echo "<br>";
}
function
updateAllUser() {
    echo
"<br>";echo "<br>";echo "<br>";
    echo
"<b>Async Update All User using parameters sets.</b>"."<br>";echo "<br>";
   
$user= new BeereOperations();
   
$data=array('id'=>[150,151,152,153,154,155]);
   
$sets =array(
       
'programme_id'=>5,
       
'programme_info'=>'Computer_Science_CSC_'
   
);
   
$result=$user->updateAll('user',$data,$sets);
   
print_r(json_decode($result,true));
    echo
"<br>";echo "<br>";
}

function
getAllUser() {
    echo
"<br>";echo "<br>";echo "<br>";
    echo
"<b>Get All User using parameters.</b>"."<br>";echo "<br>";
   
$user= new BeereOperations();
   
$data=array('programme_id'=>5,'programme_info'=>'Computer_Science_CSC_');
   
$result=$user->getAll('user',$data);
   
print_r(json_decode($result,true));
    echo
"<br>";echo "<br>";
}

function
getLastIndex() {
    echo
"<br>";echo "<br>";echo "<br>";
    echo
"<b>Get The Last Index User.</b>"."<br>";echo "<br>";
   
$user= new BeereOperations();
   
$data=array();
   
$result=$user->getLastIndex('user',$data);
   
print_r(json_decode($result,true));
    echo
"<br>";echo "<br>";
}

function
getUserByRole() {
    echo
"<br>";echo "<br>";echo "<br>";
    echo
"<b>Get User By list or an array of roles.</b>"."<br>";echo "<br>";
   
$user= new BeereOperations();
   
$data=array('SUPER_ADMIN','SCHOOL_ADMIN','BURSAR');
   
$result=$user->getByRole('user',$data,'||');
   
print_r(json_decode($result,true));
    echo
"<br>";echo "<br>";
}

function
countByParam() {
    echo
"<br>";echo "<br>";echo "<br>";
    echo
"<b>Count User By parameters.</b>"."<br>";echo "<br>";
   
$user= new BeereOperations();
   
$data=array('SUPER_ADMIN','SCHOOL_ADMIN','BURSAR');
   
$result=$user->countByParam('user',$data,'||');
   
print_r(json_decode($result,true));
    echo
"<br>";echo "<br>";
}


//Call Functions
//saveUser();
//updateUser();
//listUser();
//validateUser();
//getData();
//saveUsingMultiple();
//listByLimitPager();
//deleteUser();
//updateAllUser();
//getAllUser();
//getLastIndex();
//getUserByRole();
//countByParam();


  Files folder image Files  
File Role Description
Files folder imageexample (2 files)
Files folder imageinterface (3 files)
Files folder imageutils (2 files)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE.txt Doc. Documentation
Accessible without login Plain text file package.json Data Auxiliary data

  Files folder image Files  /  example  
File Role Description
  Accessible without login Plain text file userPdoServices.php Example PDO SAMPLE
  Accessible without login Plain text file userServices.php Example Example script

  Files folder image Files  /  interface  
File Role Description
  Plain text file BeereInterfaces.php Class Class source
  Plain text file BeereOperations.php Class Class source
  Plain text file BeerePdoOperations.php Class PDO Restful

  Files folder image Files  /  utils  
File Role Description
  Plain text file Connection.php Class Class source
  Plain text file RestfulResponse.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:403
This week:0
All time:6,536
This week:555Up
User Ratings User Comments (1)
 All time
Utility:66%StarStarStarStar
Consistency:66%StarStarStarStar
Documentation:-
Examples:66%StarStarStarStar
Tests:-
Videos:-
Overall:46%StarStarStar
Rank:3138
 
nice
7 years ago (muabshir)
80%StarStarStarStarStar