1 回答

TA貢獻(xiàn)1847條經(jīng)驗(yàn) 獲得超11個(gè)贊
正如動(dòng)態(tài)構(gòu)建 WHERE 子句。此代碼假定至少有 1 個(gè)選項(xiàng)(否則也有條件地添加 WHERE。)
每個(gè)部分連同相應(yīng)的綁定數(shù)組一起添加到列表中,這顯示了數(shù)據(jù),您需要為數(shù)據(jù)庫(kù)添加實(shí)際的 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 關(guān)注
- 132 瀏覽
添加回答
舉報(bào)