【问题标题】:PHP MySQL query in for loopfor循环中的PHP MySQL查询
【发布时间】:2014-03-21 07:36:02
【问题描述】:

我正在尝试在 for 循环中运行一个 while 循环来检查某些内容,我有这个:

for($i = 1; $i <= $days; $i++) {
while($c_cross = mysqli_fetch_array($ccc_cross)) {
    $d = explode("-", $c_cross['data']);
    $d1 = ltrim($d[2], 0);
    if($d1 == $i)
       echo "something";
    $count++;
    $sumaCross = $sumaCross + $c_cross['ccc_cross'];
    $sume[] = $c_cross['ccc_cross'];
}   

}

我想检查 $c_cross['data'] 是否等于我的 $i 变量和if so, echo something问题是while循环只运行一次for $i = 1然后就停止了。关于如何做到这一点的任何想法?

【问题讨论】:

  • $days 的值是多少?
  • 当月天数,本例为31天。
  • 只是为了确定 - echo $days 显示 31 吗?
  • 是的,echo $days 显示 31。
  • 仍然,尝试在循环之前打印echo mysqli_num_rows($ccc_cross)

标签: php for-loop while-loop


【解决方案1】:

for 循环放在while 循环内...

while($c_cross = mysqli_fetch_array($ccc_cross)) {
    $d = explode("-", $c_cross['data']);
    $d1 = ltrim($d[2], 0);
    for($i = 1; $i <= $days; $i++) {
        if($d1 == $i)
            echo "something";
    }
    $count++;
    $sumaCross = $sumaCross + $c_cross['ccc_cross'];
    $sume[] = $c_cross['ccc_cross'];
}

【讨论】:

    【解决方案2】:

    尝试在 for 循环外使用 mysqli_fetch_array。第二次 mysqli_fetch_array 相同的结果将返回空。

    或者每次都尝试在for循环中执行查询。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-31
      • 2015-02-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多