soo ..我想在我的网站上创建过滤器功能,他使用3个表tb_data,tb_peserta和paket在yhis页面上打印出表组件
$keyword = $_POST['keyword'];
$query = "
SELECT
tb_peserta.id_peserta,
tb_data.nama,
tb_paket.nama_paket,
ket,
waktu_awal,
waktu_akhir
FROM
tb_peserta,tb_data,tb_paket
WHERE
tb_peserta.id_number=tb_data.id_number
AND tb_peserta.id_paket=tb_paket.id_paket
AND status_data_peserta='aktif'
AND nama LIKE '%$keyword%'
OR nama_paket LIKE '%$keyword%'
OR ket LIKE '%$keyword'
";
那是我做过的过滤器查询..这不显示数据,但显示所有数据,它不起作用..因此请您帮忙
答案 0 :(得分:0)
您很有可能在这里创建了笛卡尔联接。我建议使用ansi风格的sql,因为它更清晰,并且不太容易受到意外的联接结果的影响:
SELECT
tb_peserta.id_peserta,tb_data.nama,tb_paket.nama_paket,ket,waktu_awal,waktu_akhir
FROM
tb_peserta
INNER JOIN tb_data on tb_peserta.id_number=tb_data.id_number
INNER JOIN tb_paket on tb_peserta.id_paket = tb_paket.id_paket
WHERE
status_data_peserta='aktif'
— note parentheses and placeholders!
AND (
nama LIKE ?
OR
nama_paket LIKE ?
OR
ket LIKE ?
)
需要注意的两件事:
and
和or
时,如果不使用括号来说明操作顺序,则会得到意想不到的结果?
)。供参考,请访问此网站以了解如何使用PDO