1

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

7
  • while($payment_owe_loop=="inside") that should be a single equal sign. You shouldn't be comparing but assigning which is what while does, and not a conditional statement, just as you did for while($rowINNER = mysql_fetch_array($result)) - You may have meant if() - if($payment_owe_loop=="inside"){...} or while($payment_owe_loop ="inside") Commented Sep 3, 2014 at 18:41
  • Hi Fred, I am trying to break out of the while loop when payment_owe_loop == the value of "inside" , It works to break out as my counter change the value of variable payment_owe_loop to outside on above value of 4. I added mysql statement echo result, I am trying debug for a hour or so but got no idea what actually went wrong in my code that my loop_userid won't overwrite until the referral 1000 Commented Sep 3, 2014 at 18:45
  • 3
    @Fred-ii- No... the reason row fetch while loops work like that is that if there is no row to fetch, it returns FALSE which breaks the loop. While continues the loop as long as the condition given is TRUE. Commented Sep 3, 2014 at 18:47
  • Is there something wrong with my code which lead to the assigning having a issue Commented Sep 3, 2014 at 18:48
  • Can you perform "select * from account where account_id='1021'" directly to make sure it returns the rows you are expecting? Commented Sep 3, 2014 at 18:55

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.