变量进入MySQL语句不起作用

I have an array that lists 14 dates from a set date (2 week pay period):

$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)
    )

I want to loop through it and return some data from a database:

    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;
        }   
    }

Thats not working but:

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

somehow (int)$date['codeDate'] is not working but I can't wrap my head around it and it's probably simple...

taylorjes

The SQL statment was correct. The Array was not:

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

fixed

Your SQL has an error. date and time after SELECT should be quoted like this because they are function names:

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