Pernyataan yang disiapkan tidak memiliki parameter karena Anda telah menyisipkan daftar ke dalam pernyataan sebelum menyiapkannya.
$array=array("item1","item2","item3","item4");
//This is dynamically filled, this is just an example
$in_list = "'".implode("','",$array)."'";//that's why i use implode
$stmt = $this->db->prepare('SELECT libelle,activite,adresse,tel,lat,lng FROM etablissements where type IN ('.$in_list.')');
Pada titik ini, pernyataan SQL yang telah Anda buat adalah:
SELECT libelle,activite,adresse,tel,lat,lng
FROM etablissements where type IN ('item1','Item2','Item3','Item4')
Karena pernyataan tidak memiliki parameter, mysqli_stmt::bind_param
gagal. Alih-alih menginterpolasi item ke dalam pernyataan (yang rentan terhadap injeksi), interpolasi string parameter, lalu ikat nilainya (yang harus disimpan terpisah).
$array=array("item1","item2","item3","item4");
if (count($in_list) > 0) {
$query = $this->db->prepare('SELECT libelle,activite,adresse,tel,lat,lng FROM etablissements WHERE type IN (' . str_repeat('?, ', count($in_list)-1) . '?)');
$args = $in_list;
array_unshift($args, str_repeat('s', count($in_list)));
call_user_func_array(array($query, 'bind_param'), $args);
$query->execute();
$query->bind_result($libelle,$activite,$adresse,$tel,$lat,$lng);
}
Antarmuka PDO untuk pengikatan lebih mudah.
$array=array("item1","item2","item3","item4");
if (count($in_list) > 0) {
$query = $this->db->prepare('SELECT libelle,activite,adresse,tel,lat,lng FROM etablissements WHERE type IN (' . str_repeat('?, ', count($in_list)-1) . '?)');
foreach ($in_list as $i => $arg) {
// query params are 1-based, so add 1 to the index
// PDO::PARAM_STR is the default type, so no need to pass 3rd arg
$query->bindValue($i+1, $arg);
}
$query->execute();
// no need to bind the result
}
Bahkan, bisa lebih sederhana dengan PDO, karena PDOStatement::execute
dapat mengambil daftar nilai parameter:
$array=array("item1","item2","item3","item4");
if (count($in_list) > 0) {
$query = $this->db->prepare('SELECT libelle,activite,adresse,tel,lat,lng FROM etablissements WHERE type IN (' . str_repeat('?, ', count($in_list)-1) . '?)');
$query->execute($in_list);
}