Mulitple Fetches in Loop using counter and array

I had a problem executing prepared queries using bindParam () with PDO in a loop. Basically, what I'm trying to do is loop through an array, and each element of the array returns data from the database. Now I understand that β†’ bindParam () should bind the variable to the request, but how does this work with arrays? Because I can not make it work: S

Here is my code:

<?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++; } ?> 
+4
source share
1 answer

Instead of using bindParam you can add an array parameter to $statement->execute as follows:

 $statement->execute(array(":id"=>$friendListIDs[$i])); 
+2
source

All Articles