PHP while循环问题不会覆盖变量

标签 php mysql

我在下面为 php 编写了这个 while 循环代码,我不知道为什么循环不断重复,所以我放了一个计数器来打破它。

在调试 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;

//此时UserId为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 而不是 $query

被sql语句蒙蔽了,漏检查了输入错误的变量

最佳答案

我在这里唯一能理解的是,由于您的 account 数据库使用 account_id 的字符串,所以某些 account_id 的尾部有空格.所以你得到了 1008 的推荐,也就是 1021,但我不确定 1021 是否在数据库中,也许它被输入为“1021”(一些尾随空格)

我很想知道直接在您的数据库上查询的结果是什么(直接通过 mysql 客户端执行)

select * from account where account_id='1021';

这实际上是否返回一行?如果没有,那么我怀疑必须修剪尾随空格。如果他们需要修剪,那么这行代码可能必须修改自:

$sql_select = "select * from account where account_id='$loop_userid'";

到:

$sql_select = "select * from account where TRIM(account_id)='$loop_userid'";

旁注:虽然我确定这与您的问题无关,但我会考虑稍微修改一下这段代码:

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

并且至少更改为:

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

我假设 account.account_id 是唯一键,并且您不能拥有超过 1 条记录。 $count0(没有这样的 account_id)或者为 1。如果您有重复的 account_id,那将导致灾难性的问题。

关于PHP while循环问题不会覆盖变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25651440/

相关文章:

php - 如何用单引号替换双引号

php - Preg_match_all 没有停在它应该在的地方

javascript - angularjs 阻止跨源请求

php - 如何像 SO 那样过滤掉危险的 HTML?

mysql - 从第三个表中选择记录,同时与其他表进行比较

mysql - 使用 where 子句替换/插入重复键 [mySQL]

php - Wordpress - Include_once 根文件

php - N 层树 PHP MYSQL 帮助

php - 为什么这个 SQL 查询没有返回正确的行?

MySQL使用垂直数据选择多个字段