0

我有一个数组,列出了设定日期(2 周支付期)的 14 个日期:

$punchCard = array();    
Array(
        [1] => Array(
                [showDate] => 2012-12-04
                [codeDate] => 20121204)
        [2] => Array(
                [showDate] => 2012-12-05
                [codeDate] => 20121205)
    .........................................
        [12] => Array(
                [showDate] => 2012-12-15
                [codeDate] => 20121215)
        [13] => Array(
                [showDate] => 2012-12-16
                [codeDate] => 20121216)
        [14] => Array(
                [showDate] => 2012-12-17
                [codeDate] => 20121217)
    )

我想遍历它并从数据库中返回一些数据:

    foreach($punchCard as $date){
        $sql = "
              SELECT date, time 
              FROM `timeclock` 
              WHERE `employee`=" . $_SESSION['id'] . " 
              AND `date`=" . (int)$date['codeDate'] . " 
              ORDER BY `time` ASC ";
        $result = mysql_query($sql);
        $timecard = array();
        while($row = mysql_fetch_assoc($result)){
            $timecard[] = $row;
        }   
    }

那是行不通的,但是:

$sql = "
     SELECT date, time 
     FROM `timeclock` 
     WHERE `employee`=" . $_SESSION['id'] . " 
     AND `date`=20121204 
     ORDER BY `time` ASC ";

不知何故(int)$date['codeDate']不起作用,但我无法理解它,这可能很简单......

泰勒耶斯

4

2 回答 2

0

您的 SQL 有错误。datetimeafterSELECT应该像这样引用,因为它们是函数名:

$sql = "
 SELECT `date`, `time` 
 FROM `timeclock` 
 WHERE `employee`=" . $_SESSION['id'] . " 
 AND `date`=20121204 
 ORDER BY `time` ASC ";
于 2012-12-07T21:50:05.227 回答
0

SQL 语句是正确的。数组不是:

while($row = mysql_fetch_assoc($result)){//show last punch
        $timecard[]['time'] = $row['time'];
    }

固定的

于 2012-12-08T00:08:44.517 回答