mysql - while loop not working for more than one variable php?
I am trying to make a while loop which retrieves all the relevant data from Mysql database but it doesn't work for more than one variable, the problem I think is with the while loop because I have echoed the sql statement and it retrieved the values of the variables right, the code is:
$wherein = implode(',', $_SESSION['cart']);
$sql = "select ID, Name, Price from lamps WHERE ID = '$wherein'";
$result = mysqli_query($conn, $sql);
echo "<table style='width:100%' border='1' >";
echo "<tr>";
echo "<th> Product Name</th>";
echo "<th>Product Price </th>" ;
echo "<th>Quantity </th>" ;
echo "</tr>";
while ( $row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo "<tr>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td> $". $row['Price'] . "</td>" ;
echo "<td> <select>
<option value= '1'>1</option>
<option value= '2'>2</option>
<option value= '3'>3</option>
</select>
</td>";
echo "</tr>";
}
echo "</table>";
I have tried many things with the code but the problem still exists, I would really appreciate your help, Thank you.
Answer
Solution:
The
</table>
statement is inside the while loop. Hence once the table is closed after the first loop the rest of the data might not be showing on the browser (it will still be in your html source code). Try it with moving the</table>
statement to the last line.Answer
Solution:
assuming that by imploding the variable you will have multiple IDs then you should use the
IN
statement in the sql.The rendered output can be simplified slightly and the table should be closed after the loop! Not knowing the contents of
$wherein
makes it tricky to answer but I would think theIN
statement seems to fit the nature of a comma separated value being used in the query better than a direct equals=