2nd Query Breaking While Loop!

by 3 replies
4
Hi

How can I rewrite this code so that the 2nd nested query doesn't break the while loop?

<?php

require('../config.php');

// Make a MySQL Connection
// Construct our join query
$query = "SELECT ura_items.ProductName, coupons.Product_Name ".
"FROM ura_items, coupons ".
"WHERE ura_items.ProductName = coupons.Product_Name limit 5 ";

$result = mysql_query($query) or die(mysql_error());

// Print out the contents of each row into a table
while($row = mysql_fetch_array($result)){

$name = $row['ProductName'];

$query = "UPDATE ura_items SET Offer='Yes' WHERE ProductName like '$name' ";
$result = mysql_query($query) or die(mysql_error());

echo $name;
echo "&nbsp;updated!";
echo "<br>";

?>

Regards
#programming #2nd #breaking #loop #query
  • It looks to me as if you just need to use different variable names in the inner loop. By using the same ones as in the outer loop, you're interrupting the outer loop.

    Something like this:

    <?php

    require('../config.php');

    // Make a MySQL Connection
    // Construct our join query
    $query = "SELECT ura_items.ProductName, coupons.Product_Name ".
    "FROM ura_items, coupons ".
    "WHERE ura_items.ProductName = coupons.Product_Name limit 5 ";

    $result = mysql_query($query) or die(mysql_error());

    // Print out the contents of each row into a table
    while($row = mysql_fetch_array($result)){

    $name = $row['ProductName'];

    $query_inner = "UPDATE ura_items SET Offer='Yes' WHERE ProductName like '$name' ";
    $result_inner = mysql_query($query_inner) or die(mysql_error());

    echo $name;
    echo "&nbsp;updated!";
    echo "<br>";

    ?>
    • [ 1 ] Thanks
    • [1] reply
    • Works great!

      I left out a } though before the ?> which confused me a bit but working now though .

      Thanks for the quick reply
      • [1] reply

Next Topics on Trending Feed