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> 

    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.




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.




assuming that by imploding the variable you will have multiple IDs then you should use theIN statement in the sql.

$wherein = implode("','", $_SESSION['cart']);
$sql = "select ID, Name, Price from lamps WHERE ID IN ( '$wherein' )";

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=

$wherein = implode("','", $_SESSION['cart']);
$sql = "select ID, Name, Price from lamps WHERE ID IN ( '$wherein' )";
$result= mysqli_query( $conn, $sql );

echo "
    <table style='width:100%' border='1' >
            <th> Product Name</th>
            <th>Product Price </th>
            <th>Quantity </th>

while ( $row = mysqli_fetch_object( $result ) ){
    echo "
                    <option value= '1'>1
                    <option value= '2'>2
                    <option value= '3'>3
echo "

/* Example of using an array of IDs and imploding to generate where conditions for IN clause */

/* session - cart */

$wherein=implode( "','", $cart );
/* correctly add quotes around each string */

$sql="select ID, Name, Price from lamps WHERE ID IN ('$wherein');";
/* use the new $wherein string within quotes */
echo $sql;

>> select ID, Name, Price from lamps WHERE ID IN ('BGL1','BJL');

People are also looking for solutions to the problem: php - .htaccess - Exclude a file from redirect


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.