rdrews Posted May 5, 2009 Share Posted May 5, 2009 Not sure if this should go in the mysql section or the php section so I'm putting it in both. Sorry if that's not what I should do... Okay, here's the code that I have... <?php function reportQuery($reportName, $accountNumber){ $rq = "SELECT * FROM $reportName WHERE AccountNum = $accountNumber"; $retq = mysql_query($rq); echo mysql_error(); return $retq; } $query = "select * from accountinfo WHERE Paid = ''"; $rt = mysql_query($query); echo mysql_error(); while($kt = mysql_fetch_array($rt)){ echo "$kt[AccountNum] <br />"; echo "<table border='1' cellspacing='1' cellpadding='1'><tr><th>Type</th><th>Account Number</th><th>Employee</th><th>Pay</th></tr>"; while($rd = mysql_fetch_array(reportQuery('adjustmentsreport', $kt[AccountNum]))){ echo "<tr><td>Adjustment</td><td>$rd[AccountNum]</td><td>$rd[Name]</td><td>yes/no</td></tr>"; } echo "</table>"; } My accountinfo table has various records with a field called AccountNum that are all unique (no duplicates). My adjustmentsreport table also has various records with a field called AccountNum that match numbers in the accountinfo table but are all again unique within the adjustmentsreport table. My problem is when I open this page and the code executes I get an infinite loop on the first matching AccountNum it finds. I have looked over the code way too many times and I can't figure out what is going on. Any help would be greatly appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/156976-solved-phpmysql-infinite-loop-problem/ Share on other sites More sharing options...
Ken2k7 Posted May 5, 2009 Share Posted May 5, 2009 That's alright, but next time, please don't post duplicate topics. It's in the rules. Read #13 - http://www.phpfreaks.com/page/rules-and-terms-of-service#toc_forum_rules. If you posted in the wrong spot, just explain it like you have it and a staff member will move it if it needs to be. When using associative arrays, you need to put quotes around the keys. Like $kt['AccountNum']. Please fix those. Quote Link to comment https://forums.phpfreaks.com/topic/156976-solved-phpmysql-infinite-loop-problem/#findComment-826904 Share on other sites More sharing options...
rdrews Posted May 5, 2009 Author Share Posted May 5, 2009 Okay, I "fixed" those but now I get an error: Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\xampplite\htdocs\webs\zen\payroll.php on line 29 Line 29 is: echo "$kt['AccountNum'] <br />"; All the code now looks like this... function reportQuery($reportName, $accountNumber){ $rq = "SELECT * FROM $reportName WHERE AccountNum = $accountNumber"; $retq = mysql_query($rq); echo mysql_error(); return $retq; } $query = "select * from accountinfo WHERE Paid = ''"; $rt = mysql_query($query); echo mysql_error(); while($kt = mysql_fetch_array($rt)){ echo "$kt['AccountNum'] <br />"; echo "<table border='1' cellspacing='1' cellpadding='1'><tr><th>Type</th><th>Account Number</th><th>Employee</th><th>Pay</th></tr>"; while($rd = mysql_fetch_array(reportQuery('adjustmentsreport', $kt['AccountNum']))){ echo "<tr><td>Adjustment</td><td>$rd['AccountNum']</td><td>$rd['Name']</td><td>yes/no</td></tr>"; } echo "</table>"; Quote Link to comment https://forums.phpfreaks.com/topic/156976-solved-phpmysql-infinite-loop-problem/#findComment-826926 Share on other sites More sharing options...
Ken2k7 Posted May 5, 2009 Share Posted May 5, 2009 You can't echo an array like that. You need curly braces around them. PHP basics. echo "{$kt['AccountNum']} <br />"; By the way, if you want to avoid these issues, it's recommended that you do this: echo $kt['AccountNum'] . " <br />"; It's much cleaner. I never like the idea of having a variable inside a string. I mean a string is a string and if you put a variable in there, it should be parsed as a variable. Quote Link to comment https://forums.phpfreaks.com/topic/156976-solved-phpmysql-infinite-loop-problem/#findComment-826947 Share on other sites More sharing options...
rdrews Posted May 5, 2009 Author Share Posted May 5, 2009 Again, I appreciate your help but I have been writing like this for a long time and have never had a problem before. I don't believe the problem has anything to do with syntax. It seems to me that there is some problem with the logic of the while statement or something. To prove this, I change the code to what you suggested and I have the exact same problem. Code now looks like this... function reportQuery($reportName, $accountNumber){ $rq = "SELECT * FROM " . $reportName . " WHERE AccountNum = " . $accountNumber; $retq = mysql_query($rq); echo mysql_error(); return $retq; } $query = "select * from accountinfo WHERE Paid = ''"; $rt = mysql_query($query); echo mysql_error(); while($kt = mysql_fetch_array($rt)){ echo $kt['AccountNum'] . "<br />"; echo "<table border='1' cellspacing='1' cellpadding='1'><tr><th>Type</th><th>Account Number</th><th>Employee</th><th>Pay</th></tr>"; while($rd = mysql_fetch_array(reportQuery('adjustmentsreport', $kt['AccountNum']))){ echo "<tr><td>Adjustment</td><td>" . $rd['AccountNum'] . "</td><td>" . $rd['Name'] . "</td><td>yes/no</td></tr>"; } echo "</table>"; } Quote Link to comment https://forums.phpfreaks.com/topic/156976-solved-phpmysql-infinite-loop-problem/#findComment-826961 Share on other sites More sharing options...
Ken2k7 Posted May 5, 2009 Share Posted May 5, 2009 Can you layout your table structures? Both of them. What fields they have etc. Quote Link to comment https://forums.phpfreaks.com/topic/156976-solved-phpmysql-infinite-loop-problem/#findComment-826973 Share on other sites More sharing options...
kickstart Posted May 5, 2009 Share Posted May 5, 2009 Hi Looking at that code it appears to me that for every loop round while($rd = mysql_fetch_array(reportQuery('adjustmentsreport', $kt['AccountNum']))) it is not going to read the next record by instead execute a new query (which will have identical results to the first query and so just keep bringing back the first record). All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/156976-solved-phpmysql-infinite-loop-problem/#findComment-827014 Share on other sites More sharing options...
Ken2k7 Posted May 5, 2009 Share Posted May 5, 2009 Ah that totally slipped my mind! Sweetness, Keith. You're the man! rdrews - what kickstart or Keith meant by that is when you run a query via mysql_query(), you're restarting the internal data pointer. Think of an array like this one - array(1,2,3) - and say I just ran it so the internal data pointer points at the first entry (the number 1). After the first iteration of your second while loop, the internal data pointer for that array should be on the number 2, you're calling the same query and executing mysql_query() function again. Once you run it again, you can say goodbye to your first iteration and the internal data pointer points to the now new result at the first position (the same number 1). You see the problem now? The fix you want is to take the query and the call to mysql_query() in that function outside of the function so the function would not keep executing the mysql_query() call. So really, you won't need the function. Understand it now? I hope I clarified it well. Ken Quote Link to comment https://forums.phpfreaks.com/topic/156976-solved-phpmysql-infinite-loop-problem/#findComment-827066 Share on other sites More sharing options...
rdrews Posted May 6, 2009 Author Share Posted May 6, 2009 Okay, thanks for the help guys. I was just trying to keep the code to a minimum using that function but I went ahead and pulled the query out of the function and it's working like a charm now. Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/156976-solved-phpmysql-infinite-loop-problem/#findComment-827225 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.