1. Go to this page and download the library: Download knifelemon/easy-query 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/ */
// 1. Build your query
$q = Builder::table('users')
->where(['status' => 'active'])
->limit(10)
->build();
// 2. Prepare the SQL statement
$stmt = $pdo->prepare($q['sql']);
// 3. Execute with parameters
$stmt->execute($q['params']);
// 4. Get results
$users = $stmt->fetchAll();
// Direct concatenation = SQL injection vulnerability!
$email = $_POST['email'];
$sql = "SELECT * FROM users WHERE email = '$email'";
// If $email is: ' OR '1'='1
// SQL becomes: SELECT * FROM users WHERE email = '' OR '1'='1'
// This returns ALL users!
$email = $_POST['email'];
$q = Builder::table('users')
->where(['email' => $email])
->build();
// SQL: SELECT * FROM users WHERE email = ?
// Params: ['user input']
// The database treats the input as data, not code
$q = Builder::table('users')
->select(['id', 'name', 'email'])
->where(['status' => 'active'])
->build();
// Result:
// sql: "SELECT id, name, email FROM users WHERE status = ?"
// params: ['active']
// Method 1: Set alias in table() method (v1.0.2.2+)
$q = Builder::table('users', 'u')
->select(['u.id', 'u.name'])
->where(['u.status' => 'active'])
->orderBy('u.created_at DESC')
->limit(10)
->build();
// Method 2: Set alias using alias() method
$q = Builder::table('users')
->alias('u')
->select(['u.id', 'u.name'])
->where(['u.status' => 'active'])
->orderBy('u.created_at DESC')
->limit(10)
->build();
// Result:
// sql: "SELECT u.id, u.name FROM users AS u WHERE u.status = ? ORDER BY u.created_at DESC LIMIT 10"
// params: ['active']
#### SELECT with JOIN
$q = Builder::table('users')
->where(['id' => 123, 'status' => 'active'])
->build();
// WHERE id = ? AND status = ?
$q = Builder::table('users')
->where([
'age' => ['>=', 18],
'score' => ['<', 100],
'name' => ['LIKE', '%john%']
])
->build();
// Result:
// sql: "SELECT * FROM users WHERE age >= ? AND score < ? AND name LIKE ?"
// params: [18, 100, '%john%']
$q = Builder::table('users')
->where([
'id' => ['IN', [1, 2, 3, 4, 5]]
])
->build();
// Result:
// sql: "SELECT * FROM users WHERE id IN (?, ?, ?, ?, ?)"
// params: [1, 2, 3, 4, 5]
$q = Builder::table('users')
->where([
'status' => ['NOT IN', ['banned', 'deleted', 'suspended']]
])
->build();
// Result:
// sql: "SELECT * FROM users WHERE status NOT IN (?, ?, ?)"
// params: ['banned', 'deleted', 'suspended']
$q = Builder::table('products')
->where([
'price' => ['BETWEEN', [100, 500]]
])
->build();
// Result:
// sql: "SELECT * FROM products WHERE price BETWEEN ? AND ?"
// params: [100, 500]
// IS NULL - check for NULL values
$q = Builder::table('users')
->where(['deleted_at' => ['IS', null]])
->build();
// Result:
// sql: "SELECT * FROM users WHERE deleted_at IS NULL"
// params: []
// IS NOT NULL - check for non-NULL values
$q = Builder::table('users')
->where(['email' => ['IS NOT', null]])
->build();
// Result:
// sql: "SELECT * FROM users WHERE email IS NOT NULL"
// params: []
// Mixed with other conditions
$q = Builder::table('users')
->where([
'kakao_sender_key' => ['IS NOT', null],
'is_delete' => 'N',
'status' => 'active'
])
->build();
// Result:
// sql: "SELECT * FROM users WHERE kakao_sender_key IS NOT NULL AND is_delete = ? AND status = ?"
// params: ['N', 'active']
// Simple OR condition
$q = Builder::table('users')
->where(['status' => 'active'])
->orWhere(['role' => 'admin'])
->build();
// WHERE status = ? AND (role = ?)
// params: ['active', 'admin']
// Multiple conditions in OR group
$q = Builder::table('users')
->where(['status' => 'active'])
->orWhere([
'role' => 'admin',
'role' => 'moderator',
'permissions' => ['LIKE', '%manage%']
])
->build();
// WHERE status = ? AND (role = ? OR role = ? OR permissions LIKE ?)
// params: ['active', 'admin', 'moderator', '%manage%']
// IN with an array of values
$q = Builder::table('users')
->where(['status' => 'active'])
->whereIn('id', [1, 2, 3, 4, 5])
->build();
// sql: "SELECT * FROM users WHERE status = ? AND id IN (?, ?, ?, ?, ?)"
// params: ['active', 1, 2, 3, 4, 5]
// IN with a subquery
$q = Builder::table('users')
->whereIn('id', Builder::table('logs')->select('user_id')->where(['type' => 'signup']))
->build();
// sql: "SELECT * FROM users WHERE id IN (SELECT user_id FROM logs WHERE type = ?)"
// params: ['signup']
// NOT IN
$q = Builder::table('users')
->whereNotIn('status', ['banned', 'deleted'])
->build();
// sql: "SELECT * FROM users WHERE status NOT IN (?, ?)"
// params: ['banned', 'deleted']
// LIKE with escaped wildcards and an explicit ESCAPE clause
$q = Builder::table('products')
->where(['category' => 'books'])
->like('title', '100%')
->build();
// sql: "SELECT * FROM products WHERE category = ? AND title LIKE ? ESCAPE '!'"
// params: ['books', '%100!%%']
// Search only the start of the value
$q = Builder::table('products')
->like('title', 'PHP', 'after')
->build();
// sql: "SELECT * FROM products WHERE title LIKE ? ESCAPE '!'"
// params: ['PHP%']
// NOT LIKE
$q = Builder::table('products')
->notLike('title', 'expired')
->build();
// sql: "SELECT * FROM products WHERE title NOT LIKE ? ESCAPE '!'"
// params: ['%expired%']
// AND group (nested parentheses)
$q = Builder::table('orders')
->where(['status' => 'open'])
->groupStart()
->where(['total' => ['>=', 100]])
->where(['priority' => 'high'])
->groupEnd()
->build();
// sql: "SELECT * FROM orders WHERE status = ? AND (total >= ? AND priority = ?)"
// params: ['open', 100, 'high']
// OR alternation via orWhere() (conditions within the same call are joined with OR)
$q = Builder::table('orders')
->where(['status' => 'open'])
->where(['total' => ['>=', 100]])
->orWhere(['priority' => 'high', 'urgent' => 1])
->build();
// sql: "SELECT * FROM orders WHERE status = ? AND total >= ? AND (priority = ? OR urgent = ?)"
// params: ['open', 100, 'high', 1]
// OR group via orGroupStart()
$q = Builder::table('users')
->where(['status' => 'active'])
->orGroupStart()
->where(['role' => 'admin'])
->where(['plan' => 'premium'])
->groupEnd()
->build();
// sql: "SELECT * FROM users WHERE status = ? OR (role = ? AND plan = ?)"
// params: ['active', 'admin', 'premium']
// Negated group
$q = Builder::table('users')
->where(['status' => 'active'])
->notGroupStart()
->where(['role' => 'guest'])
->where(['banned' => 1])
->groupEnd()
->build();
// sql: "SELECT * FROM users WHERE status = ? AND NOT (role = ? AND banned = ?)"
// params: ['active', 'guest', 1]
$q = Builder::table('orders')
->select(['user_id', 'total'])
->groupBy('user_id')
->having(['total' => ['>', 1000]])
->build();
// sql: "SELECT user_id, total FROM orders GROUP BY user_id HAVING total > ?"
// params: [1000]
$q = Builder::table('orders')
->selectCount('*', 'total_orders')
->selectSum('amount', 'total_amount')
->where(['status' => 'paid'])
->build();
// sql: "SELECT COUNT(*) AS total_orders, SUM(amount) AS total_amount FROM orders WHERE status = ?"
// params: ['paid']
$q = Builder::table('orders')
->select(['user_id'])
->selectAvg('amount')
->selectMin('amount', 'min_amount')
->selectMax('amount', 'max_amount')
->groupBy('user_id')
->build();
// sql: "SELECT user_id, AVG(amount), MIN(amount) AS min_amount, MAX(amount) AS max_amount FROM orders GROUP BY user_id"
// params: []
$q = Builder::table('users')
->distinct()
->select(['country'])
->build();
// sql: "SELECT DISTINCT country FROM users"
// params: []
// Subquery in the SELECT list
$q = Builder::table('users')
->select(['id', 'name'])
->selectSubquery(
Builder::table('orders')->selectCount('*')->where(['user_id' => 42]),
'order_count'
)
->build();
// sql: "SELECT id, name, (SELECT COUNT(*) FROM orders WHERE user_id = ?) AS order_count FROM users"
// params: [42]
// Subquery as the FROM source
$q = Builder::table('users')
->fromSubquery(
Builder::table('users')->select(['id', 'email'])->where(['status' => 'active']),
'u'
)
->where(['u.age' => ['>=', 18]])
->build();
// sql: "SELECT * FROM (SELECT id, email FROM users WHERE status = ?) AS u WHERE u.age >= ?"
// params: ['active', 18]
$q = Builder::table('active_users')
->select(['id', 'name'])
->union(Builder::table('vip_users')->select(['id', 'name']))
->build();
// sql: "SELECT id, name FROM active_users UNION SELECT id, name FROM vip_users"
// params: []
// UNION ALL keeps duplicate rows
$q = Builder::table('jan_orders')
->unionAll(Builder::table('feb_orders'))
->build();
// sql: "SELECT * FROM jan_orders UNION ALL SELECT * FROM feb_orders"
// params: []
// Multi-row INSERT
$q = Builder::table('users')
->insertBatch([
['name' => 'Alice', 'email' => '[email protected]'],
['name' => 'Bob', 'email' => '[email protected]'],
])
->build();
// sql: "INSERT INTO users (name, email) VALUES (?, ?), (?, ?)"
// params: ['Alice', '[email protected]', 'Bob', '[email protected]']
// Upsert with ON DUPLICATE KEY UPDATE
$q = Builder::table('user_stats')
->upsertBatch([
['user_id' => 1, 'views' => 10],
['user_id' => 2, 'views' => 5],
], ['user_id'])
->build();
// sql: "INSERT INTO user_stats (user_id, views) VALUES (?, ?), (?, ?) ON DUPLICATE KEY UPDATE views = VALUES(views)"
// params: [1, 10, 2, 5]
// Multi-row UPDATE via CASE WHEN
$q = Builder::table('users')
->updateBatch([
['id' => 1, 'status' => 'active'],
['id' => 2, 'status' => 'disabled'],
], 'id')
->build();
// sql: "UPDATE users SET status = CASE WHEN id = ? THEN ? WHEN id = ? THEN ? END WHERE id IN (?, ?)"
// params: [1, 'active', 2, 'disabled', 1, 2]
// Batch DELETE
$q = Builder::table('users')
->deleteBatch('id', [1, 2, 3])
->build();
// sql: "DELETE FROM users WHERE id IN (?, ?, ?)"
// params: [1, 2, 3]
$q = Builder::table('products')
->when(!empty($categoryId), function ($query) use ($categoryId) {
$query->where(['category_id' => $categoryId]);
})
->when(!empty($searchTerm), function ($query) use ($searchTerm) {
$query->like('name', $searchTerm);
})
->build();
// Reset a builder in one call (replaces manual clear*() calls)
$q = Builder::table('users')
->where(['status' => 'active'])
->build(true);
// $q is the query result; the builder is reset and ready for the next statement
// Create a base query
$baseQuery = Builder::table('users')
->select(['id', 'name', 'email'])
->where(['status' => 'active'])
->orderBy('created_at DESC');
// First query: Active users in the last 30 days
$q1 = $baseQuery
->where(['created_at' => ['>=', date('Y-m-d', strtotime('-30 days'))]])
->limit(10)
->build();
$recentUsers = executeQuery($q1);
// Clear WHERE to reuse the builder
$baseQuery->clearWhere();
// Second query: All active premium users
$q2 = $baseQuery
->where(['status' => 'active', 'plan' => 'premium'])
->limit(20)
->build();
$premiumUsers = executeQuery($q2);
// Clear specific parts
$baseQuery
->clearSelect()
->clearOrderBy()
->clearLimit();
// Third query: Count active users
$q3 = $baseQuery
->count()
->where(['status' => 'active'])
->build();
$activeCount = executeQuery($q3);
// Clear only WHERE conditions
$query->clearWhere();
// Clear only SELECT columns
$query->clearSelect();
// Clear only JOINs
$query->clearJoin();
// Clear only ORDER BY
$query->clearOrderBy();
// Clear only GROUP BY
$query->clearGroupBy();
// Clear only LIMIT and OFFSET
$query->clearLimit();
// Clear everything and start fresh
$query->clearAll();
// Base query for user list
$usersQuery = Builder::table('users')
->select(['id', 'name', 'email', 'created_at'])
->where(['status' => 'active'])
->orderBy('created_at DESC');
// Get total count
$countQuery = clone $usersQuery;
$countResult = $countQuery
->clearSelect()
->count()
->build();
$totalUsers = executeQuery($countResult)[0]['cnt'];
// Get paginated results
$page = 1;
$perPage = 20;
$offset = ($page - 1) * $perPage;
$listResult = $usersQuery
->limit($perPage, $offset)
->build();
$users = executeQuery($listResult);
// Next page - reuse the same query
$usersQuery->clearLimit();
$page = 2;
$offset = ($page - 1) * $perPage;
$nextPageResult = $usersQuery
->limit($perPage, $offset)
->build();
$nextPageUsers = executeQuery($nextPageResult);