Jump to content

While loop/query help


Mr Chris

Recommended Posts

Hi,

 

Scratching my head a little here... :-(

 

I have a while loop which contains a query.  If I run the query in MYSQL I get two results returned, but if I run it in php I get 1 result returned and its like my while loop is not working.  Anyone see anything obvious below as to why the code does not loop - or if there is some kind of issue with the query that would stop this looping:

 

Thanks

 

  $sql_one = "
  SELECT * 
  FROM orders, names
  WHERE names.order_code =$order_code
  AND orders.order_code =$order_code
  ORDER BY orders.id"; 
  $result_one = mysql_query($sql_one) or die(mysql_error());
  $row = mysql_fetch_array($result_one);

$your_name = $row['your_name'];
$your_email = $row['your_email'];

//Send email

$message = "Dear ".$your_name."\n\nYou have now confirmed your order:\n\n";

$n=1;  
while ($row = mysql_fetch_assoc($result_one))  
    {
    $item_name = $row['item_name'];
    $amount  = $row['amount'];
    $quantity = $row['quantity'];
    $total_price = $row['total_product_price'];

    $message .= "Product: ".$item_name."\n";
    $message .= "Product Price: ".$amount."\n";
    $message .= "Quantity: ".$quantity."\n";
    $message .= "Total Product Price: ".$total_price."\n\n";
$n++;
}

Link to comment
https://forums.phpfreaks.com/topic/168308-while-loopquery-help/
Share on other sites

The following line of code, immediately after the line with the mysql_query() statement, is fetching the first row from the result set and is only using the name and email from it. The rest of that row is being discarded -

 

  $row = mysql_fetch_array($result_one);

 

 

Link to comment
https://forums.phpfreaks.com/topic/168308-while-loopquery-help/#findComment-887760
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.