变量进入MySQL语句不起作用

时间:2012-12-07 21:42:50

标签: php mysql arrays multidimensional-array

我有一个数组,列出了设定日期(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']无法正常工作,但我无法绕过它并且它可能很简单......

taylorjes

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

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

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

固定