Reputation: 129
I have this while loop code below written for php, I got no idea why the loop keep repeating endlessly so I put a counter to break it out.
Upon debug the result that echo was, it seems my variable $loop_userid won't overwrite.
Referral is 1021 Referral is 1021 Referral is 1021 Referral is 1021 Referral is 1021 Referral is 1021
I have a list of sql records. for account 1021, the referral was 1000
The Idea of this is
3 Accounts
1008 > 1021 > 1000
But my loop stuck at 1021
I wonder why my $loop_userid won't overwrite with the new value that is drawn out from the database.
From mysql database select
account_id referral
1000
1008 1021
1021 1000
If i echo my sql select statement
select * from account where account_id='1008'
select * from account where account_id='1021'
select * from account where account_id='1021'
select * from account where account_id='1021'
select * from account where account_id='1021'
select * from account where account_id='1021'
Below is my code
$payment_owe_loop = "inside";
//initial userid is my own userid
$loop_userid = $UserId;
//UserId is 1008 at this point
while($payment_owe_loop=="inside")
{
$sql_select = "select * from account where account_id='$loop_userid'";
$result = mysql_query($query);
//echo $sql_select;
$loop_userid = "";
$count = mysql_num_rows($result);
if($count>0)
{
while($rowINNER = mysql_fetch_array($result))
{
$loop_userid = $rowINNER['referral'];
$my_rebate = $rowINNER['rebate'];
$my_cost = (100 - $my_rebate) * $total_cost * 0.01;
}//end while fetch inner row
}//if there rows return
echo " Referral is " . $loop_userid;
if($count==0)
{
$payment_owe_loop = "outside";
}
if($counter>4)
{
$payment_owe_loop = "outside";
}
$counter++;
unset($rowINNER);
}//end while payment loop
Issue solve. I found that I query the wrong sql statement
$sql_select = "select * from account where account_id='$loop_userid'";
$result = mysql_query($query);
It suppose be $sql_select instead of $query
Blinded by the sql statement, I miss out checking the variable that I key in wrongly
Upvotes: 1
Views: 507
Reputation: 47573
The only thing I can fathom here is that since your account
database uses a string for account_id
that some account_id
's have trailing spaces. So you get your referral for 1008 which is 1021 but I'm not sure 1021
is in the database maybe it was entered as "1021 " (some trailing spaces)
I'd be curious to know what you get as a result of this query directly on your database (just do it directly via mysql client)
select * from account where account_id='1021';
Does that actually return a row or not? If it doesn't then I suspect trailing spaces that would have to be trimmed. If they need trimming then this line of code might have to e modified from:
$sql_select = "select * from account where account_id='$loop_userid'";
to:
$sql_select = "select * from account where TRIM(account_id)='$loop_userid'";
On a side note: Although I am sure this isn't related to your problem I would consider amending this code a bit:
if($count>0)
{
while($rowINNER = mysql_fetch_array($result))
{
$loop_userid = $rowINNER['referral'];
$my_rebate = $rowINNER['rebate'];
$my_cost = (100 - $my_rebate) * $total_cost * 0.01;
}//end while fetch inner row
}//if there rows return
And at least change to:
if($count>0)
{
rowINNER = mysql_fetch_array($result)
$loop_userid = $rowINNER['referral'];
$my_rebate = $rowINNER['rebate'];
$my_cost = (100 - $my_rebate) * $total_cost * 0.01;
}//if there rows return
I am assuming that account.account_id is a unique key and that you can't have more than 1 record. Either $count
is 0 (there is no such account_id) or it is 1. If you have duplicate account_id's then that would cause catastrophic problems.
Upvotes: 3