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 關注
- 135 瀏覽
添加回答
舉報