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%']
// 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);