DATE_FORMAT省略秒

时间:2012-09-19 15:31:46

标签: php mysql

尝试将时间ID和时间本身带回存储在mysql中的列表框中。在myadmin中运行sql运行正常,在代码中尝试...不太好。带回未定义的索引错误。 感谢。

<?php
function get_times(&$a_class, &$db){
$str_sql =<<<EOT
SELECT timeId, DATE_FORMAT(tSel, '%H:%i')
FROM tb_time24 
ORDER BY timeId
EOT;
if ($query_result = mysql_query($str_sql, $db)) {

    while ($a_result = mysql_fetch_assoc($query_result)) {

        $a = array();
        $a['timeId'] = $a_result['timeId'];
        $a['tSel'] = $a_result['tSel'];

        array_push($a_class, $a);           
    }
}
else {
    $i_result = mysql_errno($db);
}
if(isset($i_result)){



return $i_result;
}   
}
?>
calling it here.

Start Time:<select name="startTime" id="StartTime">
       <?php
            $a_class = array();
            get_times($a_class, $db_handle);
            foreach ($a_class as $a_class) {
print "<option value='".$a_class['timeId']."'>{$a_class['tSel']}</option>\n";
            }
        ?>
        </select>

1 个答案:

答案 0 :(得分:4)

为格式化列命名:

$str_sql =<<<EOT
SELECT timeId, DATE_FORMAT(tSel, '%H:%i') tSel
FROM tb_time24 
ORDER BY timeId
EOT;

否则mysql_fetch_assoc返回的数组中的键将为timeIdDATE_FORMAT(tSel, '%H:%i')