for循环只返回最后的结果 - php

时间:2016-03-12 05:49:41

标签: php mysql for-loop

My For循环看起来像这样

for ( $i = $startTime; $i <= $endTime; $i = $i + 86400 ) 
{
    $thisDate = date( 'Y-m-d', $i );
    $sql = "SELECT * FROM tablename WHERE date = '$thisDate'";
    $res = parent::_executeQuery($sql);
    $rs = parent::getAll($res);         

}

return($rs);

现在return($rs)只会给我最后的记录。如何在循环中获得所有结果?

3 个答案:

答案 0 :(得分:1)

$rs=array();
for ( $i = $startTime; $i <= $endTime; $i = $i + 86400 ) 
{
    $thisDate = date( 'Y-m-d', $i );
    $sql = "SELECT * FROM tablename WHERE date = '$thisDate'";
    $res = parent::_executeQuery($sql);
    array_push($rs,parent::getAll($res));      

}

return($rs);

答案 1 :(得分:1)

你使用数组 像这样

$rs=array();
for ( $i = $startTime; $i <= $endTime; $i = $i + 86400 ) 
{
$thisDate = date( 'Y-m-d', $i );
$sql = "SELECT * FROM tablename WHERE date = '$thisDate'";
$res = parent::_executeQuery($sql);
$rs[]  = parent::getAll($res);         

}

return($rs);

答案 2 :(得分:1)

您也可以尝试这样,

$sql = "SELECT * FROM tablename WHERE date BETWEEN  '".date('Y-m-d',$startTime)."' AND  '".date('Y-m-d',$endTime)."'";
$res = parent::_executeQuery($sql);
$rs = parent::getAll($res);
return($rs);

这不需要循环,您将在单个查询中获得所有结果。