Skip to content

Latest commit

 

History

History
50 lines (33 loc) · 2.5 KB

README.md

File metadata and controls

50 lines (33 loc) · 2.5 KB

sqlHelper

This helper class returns sql string only, we need to specify the table name and data array to get sql string

To Insert a row into table

use

sqlHelper::insert($tableName, $dataArray)
function

E.g.

sqlHelper::insert('myTable', ['column1'=>'value1', 'column2'=>'value2', 'column3'=>'value3']);

this will return

"INSERT INTO myTable ('column1', 'column2', 'column3') VALUES ('value1', 'value2', 'value3')"

To select a row form table

use
sqlHelper::select($tableName, $columnArrayToBeFetched);
use
sqlHelper::select($tableName, $columnArrayToBeFetched, $where);
use
sqlHelper::select($tableName, $columnArrayToBeFetched, $limit);
use
sqlHelper::select($tableName, $columnArrayToBeFetched, $where, $limit);

$where and $limit is optional. By default $limit is 1
To fetch a range of row $limit = "$start, $amount" And to fetch all row $limit = '*'

E.g.

sqlHelper::select('myTable', ['column1', 'column2', 'column3']);
This will return
"SELECT column1, column2, column3 FROM myTable LIMIT 1"

E.g.

sqlHelper::select('myTable', ['column1'], ['column2'=>'value2', 'column3'=>'value3']);
This will return
"SELECT column1 FROM myTable WHERE column2='value2' AND column3='value3' LIMIT 1"

E.g.

sqlHelper::select('myTable', ['column1'], 2);
This will return
"SELECT column1 FROM myTable LIMIT 2"

E.g.

sqlHelper::select('myTable', ['column1'], "2, 10");
This will return
"SELECT column1 FROM myTable LIMIT 2, 10"

E.g.

sqlHelper::select('myTable', ['column1'], ['column2'=>'value2', 'column3'=>'value3'], "10, 5");
This will return
"SELECT column1 FROM myTable WHERE column2='value2' AND column3='value3' LIMIT 10, 5"

To Update Row

Use sqlHelper::update($table, $data, $where);

E.g.

sqlHelper::update('myTable', ['column2'=>'value2', 'column3'=>'value3'], ['column1'=>'value1']);
This will return
"UPDATE myTable SET column2='value2', column3='value3' WHERE column1='value1'"

To Delete Row

Use sqlHelper::delete($table, $where, $limit); $limit is optional here. Default $limit is 1

E.g.

sqlHelper::delete('myTable', ['column1'=>'value1']);
This will return
"DELETE FROM myTable WHERE column1='value1' LIMIT 1"