Jump to content

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/156976-solved-phpmysql-infinite-loop-problem/
Share on other sites

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.

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>";

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.

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>";
}

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

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.