Installation
Composer
Open a command console, enter your project directory and execute the following command to download the latest stable version of this package:, (*1)
$ composer require inglar/sql-builder
This command requires you to have Composer installed globally, as explained
in the installation chapter
of the Composer documentation., (*2)
Supported Adapters
SqlBuilder supports the following database adapters:, (*3)
- MySQL (specify mysql)
- PostgreSQL (specify pgsql)
Usage
Simple select, (*4)
$builder = new SqlBuilder('pgsql');
$select = $builder->select()
->column('*')
->from('table')
->where('id = :id')
->bindParam(':id', 123);
echo $select;
print_r($select->getBindParams());
The above example will output:, (*5)
SELECT * FROM "table" WHERE id = :id
Array
(
[:id] => 123
)
Select with join, (*6)
$builder = new SqlBuilder('pgsql');
$select = $builder->select()
->column('*')
->from('table')
->join($builder->join('table2', "table2.user_id = table.id")
->where('id = :id')
->bindParam(':id', 123);
echo $select;
print_r($select->getBindParams());
The above example will output:, (*7)
SELECT * FROM "table" JOIN "table2" ON table2.user_id = table.id WHERE id = :id
Array
(
[:id] => 123
)