My purpose is to find all the item in the table that match a collection stored in a String:
$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.')');
$stmt->bind_param("s", $in_list);
$stmt->execute();
$stmt->bind_result($libelle,$activite,$adresse,$tel,$lat,$lng);
That seems not to work perfectly, it always throwing me the following warning:
mysqli_stmt::bind_param() [<a href='mysqli-stmt.bind-param'>mysqli-stmt.bind-param</a>]: Number of elements in type definition string doesn't match number of bind variables in <b>/homepages/25/d399726988/htdocs/
If i had one parameter, it would be obvious:
$stmt = $this->db->prepare('SELECT libelle,activite,adresse,tel,lat,lng FROM etablissements where type =?');
$stmt->bind_param("s", $parameter);
But since i have to deal with that, it became a bit complicated to me.
The prepared statement has no parameters because you have interpolated the list into the statement before preparing it.
At this point, the SQL statement you have created is:
Since the statement has no parameters,
mysqli_stmt::bind_param
fails. Instead of interpolating the items into the statement (which is vulnerable to injection), interpolate a string of parameters, then bind the values (which must be kept separate).PDO's interface for binding is more straightforward.
In fact, it can be even simpler with PDO, since
PDOStatement::execute
can take a list of parameter values: