2nd Query Breaking While Loop!

by Mkj
3 replies
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
#2nd #breaking #loop #query
  • Profile picture of the author Steve Diamond
    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>";

    ?>
    Signature
    Mindfulness training & coaching online
    Reduce stress | Stay focused | Keep positive and balanced
    {{ DiscussionBoard.errors[3836963].message }}
    • Profile picture of the author Mkj
      Works great!

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

      Thanks for the quick reply
      {{ DiscussionBoard.errors[3836991].message }}
      • Profile picture of the author Mkj
        Hi

        Is there a way to rewrite this code for faster execution? Or maybe build in the ability to pause after a number of queries have been carried out. There are many 1000s of products to cycle through and it is far too hard on the server for too long a period.

        Regards

        UPDATE - Solved!

        I downloaded the coupons to a text file and now run a search for any product string using using similar to this:

        <?
        $file = file_get_contents("sitemap.txt");
        if(!strpos($file, "WinAVI All-In-One Converter")) {
        echo "String Not Found!";
        }else{
        echo "String Found!";
        }
        ?>

        Lightning fast as opposed to a mysql search.
        {{ DiscussionBoard.errors[3837800].message }}

Trending Topics