Created
August 11, 2024 11:35
-
-
Save RobinBoers/ba9d609bcccb1fb2aaff2c69702bac3e to your computer and use it in GitHub Desktop.
PHP SQL builder primitives
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function in($query, $column, $list) { | |
[$sql, $params] = $query; | |
$placeholders = implode(',', array_fill(0, count($list), '?')); | |
return ["SELECT * FROM ($sql) WHERE `$column` IN $placeholders", array_merge($params, $list)]; | |
} | |
function where($query, $column, $value) { | |
[$sql, $params] = $query; | |
return ["SELECT * FROM ($sql) WHERE `$column` = ?", [$value]]; | |
} | |
function not($query, $column, $value) { | |
[$sql, $params] = $query; | |
return ["SELECT * FROM ($sql) WHERE `$column` != ?", [$value]]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment