Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ksafranski/apiql
Simple Class for PHP MySQLi Connections with JSON Output Support
https://github.com/ksafranski/apiql
Last synced: about 1 month ago
JSON representation
Simple Class for PHP MySQLi Connections with JSON Output Support
- Host: GitHub
- URL: https://github.com/ksafranski/apiql
- Owner: ksafranski
- Created: 2012-07-06T01:08:03.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2012-10-17T14:17:03.000Z (about 12 years ago)
- Last Synced: 2024-04-15T09:19:32.634Z (8 months ago)
- Language: PHP
- Size: 97.7 KB
- Stars: 5
- Watchers: 0
- Forks: 4
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
apiQL
=====Simple Class for PHP MySQLi connections with support for both 'standard' PHP database insteracion as well as JSON-formatted output.
## Getting Started
The first thing to do is to set globals for the connection:
```php
define("MYSQL_HOST","your_host");
define("MYSQL_USER","username");
define("MYSQL_PASS","password");
define("MYSQL_BASE","database");
```
Next, include/require the class file:```php
require_once('apiQL.php');
```## Usage
To use the class instatiate it, passing in either 'true' (JSON Output) or 'false' (Standard PHP access):
```php
$apiQL= new apiQL(true);
```### Test Mode
In order to allow easy troubleshooting for SQL queries the class includes a 'test' property. By setting to **true** the SQL query will simply echo out and not be executed:
```php
$apiQL->test = true;
```
The class currently supports four methods; SELECT, INSERT, UPDATE, and DELETE:###1.) SELECT
```php
$apiQL->table = "table_name";
$apiQL->columns = array("column1","column2");
$apiQL->where = "column1='something'";
$apiQL->distinct = false;
$apiQL->limit = 5;
$apiQL->order = "column1 DESC";
$apiQL->Select();
```
'columns' and 'order' are both optional and default to '*' and 'null' respectively.If using the non-JSON mode an array is returned which can be acted on:
```php
...
$output = $apiQL->Select();if(!$apiQL->test){
// Loop through output
foreach($output as $record){
echo($record['column1'] . ' ' . $record['column2'] ... );
}
}
```
When outputting JSON the following is returned:```javascript
{
"status": "success",
"data": [
{
"column1": "some_value",
"column2": "some_value",
...
},
{
"column1": "some_value",
"column2": "some_value",
...
},
...
]
}
```
###2.) INSERT```php
$apiQL->table = "table_name";
$apiQL->data = array("column1"=>"value1","column2"=>"value2", ... );
$apiQL->Insert();
```
Using the non-JSON mode the ID of the inserted element is returned:```php
...
$id = $apiQL->Insert();
```
When outputting JSON the following is returned:```javascript
{"status":"success","data":{"id":"some_value"}}
```
###3.) UPDATE```php
$apiQL->table = "table_name";
$apiQL->data = array("column"=>"value", ... );
$apiQL->where = "column='condition'";
$apiQL->Update();
```
The 'where' is opional. The function returns 'success' on completion either in string format (non-JSON) or in JSEND format```javascript
{"status":"success","data":null}
```###4.) DELETE
```php
$apiQL->table = "table_name";
$apiQL->where = "column='condition';
$apiQL->Delete();
```
The 'where' is optional. The function returns 'success' on completion either in string format (non-JSON) or in JSEND format```javascript
{"status":"success","data":null}
```## Close Connection
To close the connection simply apply the following command:
```php
$apiQL->Close();
```