1. Go to this page and download the library: Download changhorizon/sql-condition 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/ */
use ChangHorizon\SqlCondition\Condition;
use ChangHorizon\SqlCondition\Expression;
$cond = new Condition([
Expression::EQ('name', 'John'),
Expression::GT('age', '18'),
Expression::IS_NULL('deleted_at'),
]);
$cond->getString(); // "name = :value_name AND age > :value_age AND deleted_at IS NULL"
$cond->getParams(); // [':value_name' => 'John', ':value_age' => '18']
use ChangHorizon\SqlCondition\Condition;
use ChangHorizon\SqlCondition\Enums\Logic;
$cond = new Condition([
Expression::EQ('role', 'admin'),
Expression::EQ('role', 'moderator'),
], Logic::OR);
$cond->getString(); // "role = :value_role OR role = :value_role"
$adminOrMod = new Condition([
Expression::EQ('role', 'admin'),
Expression::EQ('role', 'moderator'),
], Logic::OR);
$cond = new Condition([
$adminOrMod,
Expression::IS_TRUE('is_active'),
]);
$cond->getString(); // "(role = :value_role OR role = :value_role) AND is_active IS TRUE"
use ChangHorizon\SqlCondition\Enums\Anchor;
use ChangHorizon\SqlCondition\Expressions\Like;
// Default (both sides): %value%
new Like('title', 'hello')->getParams(); // [':value_title' => '%:hello%']
// Left anchor only: value%
new Like('title', 'hello', Anchor::LEFT)->getParams(); // [':value_title' => ':hello%']
// Right anchor only: %value
new Like('title', 'hello', Anchor::RIGHT)->getParams(); // [':value_title' => '%:hello']
$cond = new Condition([
Expression::EQ('email', '[email protected]'),
Expression::IS_NOT_NULL('verified_at'),
]);
$sql = 'SELECT * FROM users WHERE ' . $cond->getString();
$stmt = $pdo->prepare($sql);
$stmt->execute($cond->getParams());