I’ve been having an issue executing prepared queries using bindParam() with PDO in a loop. Basically what I’m trying to do is loop through an array, and with each array element, return the data from the database. Now I realise that ->bindParam() is supposed to bind the variable to the query, but how does this work with arrays? Because I can’t seem to get it working :S
Here’s my code so far:
<?php
$i = 0;
$statement = $conn->prepare("SELECT * FROM users WHERE id = :id");
$statement->bindParam(":id", $friendListIDs[$i], PDO::PARAM_STR);
$friendListIDs = explode($details['friends'], " ");
while($i <= count($friendListIDs))
{
$statement->execute();
$row = $statement->fetch();
echo "<img src='../img/friend_icon.png' alt='' align='left' />
<span>
<a href='#'>".$row['firstname']." ".$row['surname']."</a>
<br />
<a href='#'>100% wishes fulfilled</a>
</span><br /><br />";
$i++;
}
?>
Rather than use
bindParamyou can add an array parameter to$statement->executelike so: