1 回答

TA贡献1847条经验 获得超11个赞
正如动态构建 WHERE 子句。此代码假定至少有 1 个选项(否则也有条件地添加 WHERE。)
每个部分连同相应的绑定数组一起添加到列表中,这显示了数据,您需要为数据库添加实际的 API 部分。implode()WHERE 部分使用withAND作为胶水放在一起......
$binds = [];
$where = [];
$types = '';
if (isset($_GET['foo'])) {
$where[] = "foo=?";
$binds[] = $_GET['foo'];
$types .= "s";
}
if (isset($_GET['bar'])) {
$where[] = "bar=?";
$binds[] = $_GET['bar'];
$types .= "s";
}
if (isset($_GET['gux'])) {
$where[] = "gux=?";
$binds[] = $_GET['gux'];
$types .= "i";
}
$sql = "SELECT * FROM ... WHERE " . implode(" AND ", $where);
echo $sql.PHP_EOL;
print_r($binds);
在 foo 值中使用 1 给出...
SELECT * FROM ... WHERE foo=?
Array
(
[0] => 1
)
bind_param()然后在...中使用类型和绑定
bind_param($types, ...$binds);
- 1 回答
- 0 关注
- 148 浏览
添加回答
举报