我有这个while循环代码为PHP编写,我不知道为什么循环不断重复,所以我把一个柜台打破它。PHP虽然循环问题不会覆盖变量
在调试echo的结果时,看起来我的变量$ loop_userid不会被覆盖。
Referral is 1021 Referral is 1021 Referral is 1021 Referral is 1021 Referral is 1021 Referral is 1021
我有一个sql记录列表。帐户1021,提名为1000
这样做的想法是
3 Accounts
1008 > 1021 > 1000
But my loop stuck at 1021
我不知道为什么我的$ loop_userid不会与从数据库中提取出来的新值覆盖。
从MySQL数据库中选择
account_id referral
1000
1008 1021
1021 1000
如果我赞同我的SQL SELECT语句
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'
下面是我的代码
$payment_owe_loop = "inside";
//initial userid is my own userid
$loop_userid = $UserId;
//用户ID是1008在这一点上
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
问题解决。我发现,我查询了错误的SQL语句
$sql_select = "select * from account where account_id='$loop_userid'";
$result = mysql_query($query);
它假设为$ sql_select中,而不是$查询
由SQL语句所蒙蔽,我错过了检查变量I键在错误
'while($ payment_owe_loop ==“inside”)''应该是单个等号。你不应该比较,而是分配哪些是'while',而不是条件语句,就像你为'while($ rowINNER = mysql_fetch_array($ result))'所做的那样。 - 'if($ payment_owe_loop ==“inside”){...}'或'while($ payment_owe_loop =“inside”)' – 2014-09-03 18:41:46
嗨Fred,我试图摆脱while循环payment_owe_loop ==值当我的计数器将变量payment_owe_loop的值更改为4以外的值时,它有效。当我添加mysql语句echo结果时,我试图调试一个小时左右,但不知道实际上发生了什么在我的代码错误,我的loop_userid不会覆盖,直到引用1000 – user3412075 2014-09-03 18:45:04
@ Fred-ii-没有... while循环工作的原因是,如果没有行可以获取,它返回FALSE它打破循环。只要给定的条件为TRUE,继续循环。 – HamHamJ 2014-09-03 18:47:32