1. Go to this page and download the library: Download ramphor/sql library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
ramphor / sql example snippets
echo sql('SELECT * FROM @ WHERE @ = ? OR name IN ([?]) OR id IN ([]) AND created = @',
'users', 'name', 'Trevor', ['Tom', 'Dick', 'Harry'], [1, 2, 3], 'NOW()');
class Sql
{
function select(...$cols)
{
$this->sql .= 'SELECT ' . implode(', ', $cols);
return $this;
}
function from(...$tables)
{
$this->sql .= PHP_EOL . 'FROM ' . implode(', ', $tables);
return $this;
}
function leftJoin($stmt, ...$params)
{
return $this->prepare(PHP_EOL . 'LEFT JOIN ', ...$params);
}
function where($stmt, ...$params)
{
return $this->prepare(PHP_EOL . 'WHERE ', ...$params);
}
function prepare($stmt, ...$params)
{
// the magic happens here
// processing `?`, `@`, escaping, quoting etc.
}
}
echo sql()->select('*')
->from('users u')
->leftJoin('accounts a ON a.user_id = u.id')
->where('u.id = ?', 5);
use Ramphor;
$sql = new sql();
$sql = new Sql();
$sql = new SQL();
// or
$sql = new \Ramphor\Sql();
function sql($stmt = null, ...$params)
{
return new Ramphor\Sql($stmt, ...$params);
}
$sql = sql();
$sql = Sql();
$sql = SQL();
->select('col1', 'col2', 'col3')
->from('table t')
->join('table2 t2 ON ... = ?', $var)
->leftJoin('table3 t3 ON ... = ?', $var)
->where('foo = ?', 'bar')
->groupBy('t.col1', 't2.col2')
->orderBy('t.col1 DESC')
->limit(5, 10);
// other common functions
->selectDistinct(..)
->insert(..)
->insertInto(..)
->values(..)
->set(..)
->delete(..)
->deleteFrom(..)
->having(..)
->union(..)
->select('col1', 'col2', 'col3')
->from('table t')
->join('table2 t2 ON ... = ?', $var)
->left_join('table3 t3 ON ... = ?', $var)
->where('foo = ?', 'bar')
->group_by('t.col1', 't2.col2')
->order_by('t.col1 DESC')
->limit(5, 10);
// other common functions
->select_distinct(..)
->insert(..)
->insert_into(..)
->values(..)
->set(..)
->delete(..)
->delete_from(..)
->having(..)
->union(..)
->SELECT('col1', 'col2', 'col3')
->FROM('table t')
->JOIN('table2 t2 ON ... = ?', $var)
->LEFT_JOIN('table3 t3 ON ... = ?', $var)
->WHERE('foo = ?', 'bar')
->GROUP_BY('t.col1', 't2.col2')
->ORDER_BY('t.col1 DESC')
->LIMIT(5, 10);
// other common functions
->SELECT_DISTINCT(..)
->INSERT(..)
->INSERT_INTO(..)
->VALUES(..)
->SET(..)
->DELETE(..)
->DELETE_FROM(..)
->HAVING(..)
->UNION(..)
->s('col1', 'col2', 'col3') // s = SELECT
->f('table t') // f = FROM
->j('table2 t2 ON ... = ?', $var) // j = JOIN
->lj('table3 t3 ON ...?', $var) // lj = LEFT JOIN
->w('foo = ?', 'bar') // w = WHERE
->gb('t.col1', 't2.col2') // gb = GROUP BY
->ob('t.col1 DESC') // ob = ORDER BY
->l(5, 10); // l = LIMIT
// other common functions
->sd(..) // sd = SELECT DISTINCT
->i(..) // i = INSERT
->ii(..) // ii = INSERT INTO
->v(..) // v = VALUES
->d(..) // d = DELETE
->df(..) // df = DELETE FROM
->h(..) // h = HAVING
\Ramphor\Sql::setConnection($conn);
$array = sql('SELECT ...')->fetchAll();
function ($sql) use ($conn)
{
$recset = $conn->query($sql);
if ( ! $recset) {
throw new \Exception('PSO::query() error: ' . $conn->errorInfo()[2]);
}
$result = $recset->fetchAll(\PDO::FETCH_ASSOC);
$recset->closeCursor();
return $result;
};