Whats stopping this loop?

  • RedBMedia
  • Proficient
  • Proficient
  • User avatar
  • Posts: 315

Post 3+ Months Ago

Ok so I got a script from a site. I need it to run for each entry in a MySQL DB. I have tried using:
Code: [ Select ]
 
while($row = mysql_fetch_array($array))
{
include 'update.php';
}
 
  1.  
  2. while($row = mysql_fetch_array($array))
  3. {
  4. include 'update.php';
  5. }
  6.  

&
Code: [ Select ]
 
while($row = mysql_fetch_array($array))
{
update();
}
 
  1.  
  2. while($row = mysql_fetch_array($array))
  3. {
  4. update();
  5. }
  6.  


With update() being a function wrapped around the contents of update.php

&

Code: [ Select ]
 
while($row = mysql_fetch_array($array))
{
// The full script of update.php pasted here
}
 
  1.  
  2. while($row = mysql_fetch_array($array))
  3. {
  4. // The full script of update.php pasted here
  5. }
  6.  


All of these only run through the first entry. Is there anything that I need to look for in this script that might be causing this loop to stop? I checked and there isn't any MySQL close statements. I feel like if this was my script I would know exactly what the problem is, but I am lost. Any help would be great!!!
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

The first thing I'd do is print the results of a mysql_num_rows statement above that loop to see how many rows are being returned.
If it's < 2 I'd look into why it's not returning as many rows as it should.
If it's > 1 I'd look within the included code to see if there's conflicting mysql code in there somewhere.
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

Make sure you are not resetting the $row in the loop
  • RedBMedia
  • Proficient
  • Proficient
  • User avatar
  • Posts: 315

Post 3+ Months Ago

@joebert Took your advice: mysql_num_rows returns 6, like its supposed to. Therefore I looked and sure enough there's other MySQL calls being made in the script. But they are all essential to operation. Why would they stop the loop? Is there something in the MySQL calls that I should look for?

@Bogey - what do you mean resetting the $row array? How would that happen?
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

Quote:
Why would they stop the loop?


The most obvious one would look something like this.

Code: [ Select ]
$result = mysql_query(...);
while($row = mysql_fetch_array($result))
{
// ...
$result = mysql_query(...);
while($row = ...){}
}
  1. $result = mysql_query(...);
  2. while($row = mysql_fetch_array($result))
  3. {
  4. // ...
  5. $result = mysql_query(...);
  6. while($row = ...){}
  7. }


Notice how $result is overwritten by the inner call to mysql_query ?
That would reduce the number of rows the outter loop worked with, if the inner calls query only returned a single row it could explain why you're only getting one run of the outter loop.
  • RedBMedia
  • Proficient
  • Proficient
  • User avatar
  • Posts: 315

Post 3+ Months Ago

I think I am getting closer here...but not out of the water yet. I wanted to see if it was the second set of MySQL commands that are stopping the loop. So, I took them out and replaced them will a simple echo statement. Sure enough the loop didn't stop this time and went completely through. Which means that it must be that second set of MySQL commands. @joebert, you mentioned that a problem that might be occurring is duplicate sql commands or duplicate strings with different commands. I took another look at the MySQL commands that are being made in the loop, and they are all different than the ones outside the loop, furthermore all of the strings are unique too. So, at this point I am lost.... @bogey did you mention about resetting? What exactly is that? How would it reset?

Thank you both so much for your help so far!
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

What I meant about resetting is what Joebert already explained.
PHP Code: [ Select ]
<?php
while($row = something)
{
   $row = 'set as something new';
}
?>
  1. <?php
  2. while($row = something)
  3. {
  4.    $row = 'set as something new';
  5. }
  6. ?>

If you don't mind pasting the code of update.php here and letting us see it... then we might be of more help.
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

It's not soo much the SQL itself, as it is the way the PHP mysql_* functions are being used and how references are being saved for the return values.

If you post the parts of your code that use mysql_* functions, and keep the scope intact, it will be easier to point out any conflicts. I know bogey will spot them if I'm not around. :)

When I say keep the scope intact, that would mean if you have something like this
Code: [ Select ]
echo '$stuff;
$array = mysql_query($sql);
while($row = mysql_fetch_array($array))
{
    echo $stuff_two;
    $sql = work_with($row);
    $array = mysql_query($sql);
    while($row = mysql_fetch_array($array))
    {
        $stuff2 = more_work_with($row);
        echo $stuff2;
    }
}
  1. echo '$stuff;
  2. $array = mysql_query($sql);
  3. while($row = mysql_fetch_array($array))
  4. {
  5.     echo $stuff_two;
  6.     $sql = work_with($row);
  7.     $array = mysql_query($sql);
  8.     while($row = mysql_fetch_array($array))
  9.     {
  10.         $stuff2 = more_work_with($row);
  11.         echo $stuff2;
  12.     }
  13. }


You would post

Code: [ Select ]
$array = mysql_query($sql);
while($row = mysql_fetch_array($array))
{
    $array = mysql_query($sql);
    while($row = mysql_fetch_array($array))
    {
    }
}
  1. $array = mysql_query($sql);
  2. while($row = mysql_fetch_array($array))
  3. {
  4.     $array = mysql_query($sql);
  5.     while($row = mysql_fetch_array($array))
  6.     {
  7.     }
  8. }
  • RedBMedia
  • Proficient
  • Proficient
  • User avatar
  • Posts: 315

Post 3+ Months Ago

What you see below is part of update.php I have changed the string names and database table info for security purposes.

Here's what the below code does. It takes strings that have been created with the use of a third party API and compares them to entries in the database. If there isn't a duplicate entry in the database then it inserts the entry into the database and then emails an alert to the user. $acctid, $user, and $useremail are all taken from the original MySQL commands that the failed loop is based on. $url and &title are filled from the third party API.

Like I said before, this whole process works fine with one rotation of the loop, but never anymore.

Code: [ Select ]
<?php
$result = mysql_query("SELECT * FROM userdata WHERE url='$url'");
$num_rows = mysql_num_rows($result);
if($num_rows==0){
$earchresult = mysql_query("INSERT INTO userdata (acctid, usererid, url, title, date) VALUES ('$acctid', '$user', '$url', '$title', NOW())");
$Body .= "<a href=\"";
$Body .= "$url";
$Body .= "\" target=\"_blank\">";
$Body .= "$title";
$Body .= "</a>";
$Body .= "<br/>";
$Body .= "<br/>";
}
$HTML = stripslashes($Body);
$from = "Updates";
$to = $useremail;
$subject = "database update notice";
if($earchresult){
$sent = sendHTMLemail($HTML,$from,$to,$subject);
}

function sendHTMLemail($HTML,$from,$to,$subject)
{
    $headers = "From: $from\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $boundary = uniqid("HTMLEMAIL");
    $headers .= "Content-Type: multipart/alternative;".
                "boundary = $boundary\r\n\r\n";
    $headers .= "This is a MIME encoded message.\r\n\r\n";
    $headers .= "--$boundary\r\n".
                "Content-Type: text/plain; charset=ISO-8859-1\r\n".
                "Content-Transfer-Encoding: base64\r\n\r\n";
    $headers .= chunk_split(base64_encode(strip_tags($HTML)));
    $headers .= "--$boundary\r\n".
                "Content-Type: text/html; charset=ISO-8859-1\r\n".
                "Content-Transfer-Encoding: base64\r\n\r\n";
    $headers .= chunk_split(base64_encode($HTML));
    mail($to,$subject,"",$headers);
}

?>
  1. <?php
  2. $result = mysql_query("SELECT * FROM userdata WHERE url='$url'");
  3. $num_rows = mysql_num_rows($result);
  4. if($num_rows==0){
  5. $earchresult = mysql_query("INSERT INTO userdata (acctid, usererid, url, title, date) VALUES ('$acctid', '$user', '$url', '$title', NOW())");
  6. $Body .= "<a href=\"";
  7. $Body .= "$url";
  8. $Body .= "\" target=\"_blank\">";
  9. $Body .= "$title";
  10. $Body .= "</a>";
  11. $Body .= "<br/>";
  12. $Body .= "<br/>";
  13. }
  14. $HTML = stripslashes($Body);
  15. $from = "Updates";
  16. $to = $useremail;
  17. $subject = "database update notice";
  18. if($earchresult){
  19. $sent = sendHTMLemail($HTML,$from,$to,$subject);
  20. }
  21. function sendHTMLemail($HTML,$from,$to,$subject)
  22. {
  23.     $headers = "From: $from\r\n";
  24.     $headers .= "MIME-Version: 1.0\r\n";
  25.     $boundary = uniqid("HTMLEMAIL");
  26.     $headers .= "Content-Type: multipart/alternative;".
  27.                 "boundary = $boundary\r\n\r\n";
  28.     $headers .= "This is a MIME encoded message.\r\n\r\n";
  29.     $headers .= "--$boundary\r\n".
  30.                 "Content-Type: text/plain; charset=ISO-8859-1\r\n".
  31.                 "Content-Transfer-Encoding: base64\r\n\r\n";
  32.     $headers .= chunk_split(base64_encode(strip_tags($HTML)));
  33.     $headers .= "--$boundary\r\n".
  34.                 "Content-Type: text/html; charset=ISO-8859-1\r\n".
  35.                 "Content-Transfer-Encoding: base64\r\n\r\n";
  36.     $headers .= chunk_split(base64_encode($HTML));
  37.     mail($to,$subject,"",$headers);
  38. }
  39. ?>


Thanks again for your help guys!
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

I'm confused, is that just the code inside the loop that you're trying to add ?

If it is, it's not possible to find conflicts without seeing the mysql-specific parts of the outter loop too.
  • RedBMedia
  • Proficient
  • Proficient
  • User avatar
  • Posts: 315

Post 3+ Months Ago

Oops! My bad!! Heres the full code:

Code: [ Select ]
<?php


// third party API code that fills values for $url and $title goes here


$con = mysql_connect("localhost","db_user","db_pass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("data", $con);

$sql = "SELECT * FROM acct_configs";
$array = mysql_query($sql,$con);
while($row = mysql_fetch_array($array))
{

$acctid = $row[acctid];
$user = $row[usererid];
$useremail = $row[email];

// from the code I pasted above

$result = mysql_query("SELECT * FROM userdata WHERE url='$url'");
$num_rows = mysql_num_rows($result);
if($num_rows==0){
$earchresult = mysql_query("INSERT INTO userdata (acctid, usererid, url, title, date) VALUES ('$acctid', '$user', '$url', '$title', NOW())");
$Body .= "<a href=\"";
$Body .= "$url";
$Body .= "\" target=\"_blank\">";
$Body .= "$title";
$Body .= "</a>";
$Body .= "<br/>";
$Body .= "<br/>";
}
$HTML = stripslashes($Body);
$from = "Updates";
$to = $useremail;
$subject = "database update notice";
if($earchresult){
$sent = sendHTMLemail($HTML,$from,$to,$subject);
}

function sendHTMLemail($HTML,$from,$to,$subject)
{
    $headers = "From: $from\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $boundary = uniqid("HTMLEMAIL");
    $headers .= "Content-Type: multipart/alternative;".
                "boundary = $boundary\r\n\r\n";
    $headers .= "This is a MIME encoded message.\r\n\r\n";
    $headers .= "--$boundary\r\n".
                "Content-Type: text/plain; charset=ISO-8859-1\r\n".
                "Content-Transfer-Encoding: base64\r\n\r\n";
    $headers .= chunk_split(base64_encode(strip_tags($HTML)));
    $headers .= "--$boundary\r\n".
                "Content-Type: text/html; charset=ISO-8859-1\r\n".
                "Content-Transfer-Encoding: base64\r\n\r\n";
    $headers .= chunk_split(base64_encode($HTML));
    mail($to,$subject,"",$headers);
}

}
?>
  1. <?php
  2. // third party API code that fills values for $url and $title goes here
  3. $con = mysql_connect("localhost","db_user","db_pass");
  4. if (!$con)
  5. {
  6. die('Could not connect: ' . mysql_error());
  7. }
  8. mysql_select_db("data", $con);
  9. $sql = "SELECT * FROM acct_configs";
  10. $array = mysql_query($sql,$con);
  11. while($row = mysql_fetch_array($array))
  12. {
  13. $acctid = $row[acctid];
  14. $user = $row[usererid];
  15. $useremail = $row[email];
  16. // from the code I pasted above
  17. $result = mysql_query("SELECT * FROM userdata WHERE url='$url'");
  18. $num_rows = mysql_num_rows($result);
  19. if($num_rows==0){
  20. $earchresult = mysql_query("INSERT INTO userdata (acctid, usererid, url, title, date) VALUES ('$acctid', '$user', '$url', '$title', NOW())");
  21. $Body .= "<a href=\"";
  22. $Body .= "$url";
  23. $Body .= "\" target=\"_blank\">";
  24. $Body .= "$title";
  25. $Body .= "</a>";
  26. $Body .= "<br/>";
  27. $Body .= "<br/>";
  28. }
  29. $HTML = stripslashes($Body);
  30. $from = "Updates";
  31. $to = $useremail;
  32. $subject = "database update notice";
  33. if($earchresult){
  34. $sent = sendHTMLemail($HTML,$from,$to,$subject);
  35. }
  36. function sendHTMLemail($HTML,$from,$to,$subject)
  37. {
  38.     $headers = "From: $from\r\n";
  39.     $headers .= "MIME-Version: 1.0\r\n";
  40.     $boundary = uniqid("HTMLEMAIL");
  41.     $headers .= "Content-Type: multipart/alternative;".
  42.                 "boundary = $boundary\r\n\r\n";
  43.     $headers .= "This is a MIME encoded message.\r\n\r\n";
  44.     $headers .= "--$boundary\r\n".
  45.                 "Content-Type: text/plain; charset=ISO-8859-1\r\n".
  46.                 "Content-Transfer-Encoding: base64\r\n\r\n";
  47.     $headers .= chunk_split(base64_encode(strip_tags($HTML)));
  48.     $headers .= "--$boundary\r\n".
  49.                 "Content-Type: text/html; charset=ISO-8859-1\r\n".
  50.                 "Content-Transfer-Encoding: base64\r\n\r\n";
  51.     $headers .= chunk_split(base64_encode($HTML));
  52.     mail($to,$subject,"",$headers);
  53. }
  54. }
  55. ?>
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

What happens if you move the declaration of your "sendHTMLemail" function outside of the loop ?

I'm thinking the loop is choking when it comes to define that function for the second time.
  • RedBMedia
  • Proficient
  • Proficient
  • User avatar
  • Posts: 315

Post 3+ Months Ago

@joebert - I don't think that a heterosexual man has ever felt as much love for another man, as I do for you at this moment! YOU ARE AWESOME!!!! Some one pass him a beer! :starwars:

Post Information

  • Total Posts in this topic: 13 posts
  • Users browsing this forum: No registered users and 101 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.