Guy in the chair
Guy in the chair

Reputation: 1065

Nested while loop not working with mysql database queries

What I want to achieve is create a table with order id & related product information. If one order id contains more than one product, order id should be printed only once in first row while for further rows only product information should be present starting from second column. I tried nearly couple of hours working on this but couldn't find a solution. Below is the code:

$query3= "select orderid from orders_main where userid=$cus_id";
    $results3= mysql_query($query3);
    while ($row = @ mysql_fetch_array($results3))
    {
        echo "<tr>";
            echo "<th width:17%>".$row["orderid"]."</th>";
        $temporderid=$row["orderid"];
        $query2 = "SELECT product_name, quantity, subtotal from orders where orderid='$temporderid'";
        $result2 = mysql_query($query2);
    while ($row2 = @ mysql_fetch_array($result2))
        {
            echo "<th width:35%>".$row2["product_name"]."</th>";
            echo "<th width:13%>".$row2["subtotal"]."</th>";
            echo "<th width:22%></th>";
        echo "</tr>";
                            echo "<th></th>";
        }
    }
        echo "</table>";
    }

What happens is it only prints the order id & not any product information. Any help is appreciated.

Upvotes: 0

Views: 1225

Answers (1)

Marc B
Marc B

Reputation: 360862

Typo:

    $result2 = mysql_query($query2);
           ^---no S

while ($row2 = @ mysql_fetch_array($results2))
                                          ^--- S

If you weren't using tha @#%@#%@#$$#@@#$%@#$@# @ error suppression operator, you'd have gotten the "attempting to fetch from ..." error.

Never EVER use that moronic operator. ESPECIALLY when you're having trouble getting the code to work. YOu've done the equivalent of stuffing your fingers in your hears and chanting "lalalalala can't hear you.... "

Upvotes: 2

Related Questions