This small library, allows you to create simple SQL queries to use with PDO easily. Just using methods with simple db command names, you can create seamless statements and key value array to use in execution.
When using PDO, writing queries are prone to syntax and parameter errors. To prevent them in simple queries you can use this library.
Strictly requires PHP 7.4.
Via Composer
$ composer require midorikocak/querymaker
There are starter methods to create a query, such as SELECT
and UPDATE
.
$queryMaker = new QueryMaker();
$queryMaker->select('users');
echo $queryMaker->getQuery();
The above example will output:
SELECT * FROM users
Fields to select can be specified as well:
$queryMaker = new QueryMaker();
$queryMaker->select('users', ['id', 'email']);
echo $queryMaker->getQuery();
The above example will output:
SELECT id, email FROM users
Field values can include operators, such as: =
,>
, <
,<=
,>=
$queryMaker = new QueryMaker();
$queryMaker->select('users', ['id', 'email'])->where('id', '3', '>=');
echo $queryMaker->getQuery();
echo $queryMaker->getStatement();
The above example will output:
SELECT id, email FROM users WHERE id>='3'
SELECT id, email FROM users WHERE id>=:id'
$queryMaker = new QueryMaker();
$queryMaker->delete('users');
echo $queryMaker->getQuery();
The above example will output:
DELETE FROM users
To specify WHERE
clauase use where($key, $value)
method.
$queryMaker = new QueryMaker();
$queryMaker->select('users', ['id', 'email'])->where('id', 3);
echo $queryMaker->getQuery();
echo $queryMaker->getStatement();
The above example will output:
SELECT id, email FROM users WHERE id='3'
SELECT id, email FROM users WHERE id=:id
Contraints such as AND
and OR
, are methods as well. and($key, $value)
and or($key, $value)
$queryMaker = new QueryMaker();
$queryMaker->select('users', ['id', 'email'])->where('id', 3)->and('email', 'mtkocak@gmail.com')->or('username', 'midori');
echo $queryMaker->getQuery();
echo $queryMaker->getStatement();
The above example will output:
SELECT id, email FROM users WHERE id='3' OR username='midori'
SELECT id, email FROM users WHERE id=:id OR username=:username
Multiple AND and OR clauses can have same field conditions.
$queryMaker = new QueryMaker();
$queryMaker->select('users', ['id', 'email'])->where('email', 'mtkocak@gmail.com')->and('id', '>3')->and('id', '<5');
To specify ORDER BY
clauase use order($key, $order)
method.
$queryMaker = new QueryMaker();
$queryMaker->select('users')->orderBy('id');
echo $queryMaker->getQuery();
The above example will output:
SELECT * FROM users ORDER BY id ASC
To specify OFFSET
and LIMIT
clauase use offset($offset)
and limit($offset)
methods.
$queryMaker = new QueryMaker();
$queryMaker->select('users')->orderBy('id')->limit(3)->offset(2);
echo $queryMaker->getQuery();
The above example will output:
SELECT * FROM users ORDER BY id ASC LIMIT 3 OFFSET 2
It's also possible to get values as key value pair to easily execute.
$db = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
$queryMaker = new QueryMaker();
$queryMaker->select('users', ['id', 'email'])->where('id', 3)->and('email', 'mtkocak@gmail.com')->or('username', 'midori');
$statement = $db->prepare($query->getStatement());
$statement->execute($query->getParams());
To specify INSERT
operation, insert()
method, expects a key value array.
$queryMaker = new QueryMaker();
$queryMaker->insert('users', ['email' => 'mtkocak@gmail.com', 'username' => 'midorikocak']);
echo $queryMaker->getQuery();
echo $queryMaker->getStatement();
The above example will output:
INSERT INTO users (email, username) VALUES ('mtkocak@gmail.com', 'midorikocak')
INSERT INTO users (email, username) VALUES (:email, :username)
To specify UPDATE
operation, handy update()
method, expects a key value array. All statement params are generated thoroughly.
$queryMaker = new QueryMaker();
$queryMaker->update('users', ['email' => 'mtkocak@gmail.com', 'username' => 'midorikocak'])->where('id', 3);
echo $queryMaker->getQuery();
echo $queryMaker->getStatement();
The above example will output:
UPDATE users SET email='mtkocak@gmail.com', username='midorikocak' WHERE id='3'
UPDATE users SET email=:email, username=:username WHERE id=:id
This library is for educational purposes. Use at your own risk. Exposing query values and using it would create security issues.
Please see CHANGELOG for more information on what has changed recently.
$ composer test
Please see CONTRIBUTING and CODE_OF_CONDUCT for details.
If you discover any security related issues, please email mtkocak@gmail.com instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.