2015-12-25 82 views
0

我正在使用SQL來顯示當前正由此人和此教師教授的實際主題。它應該只顯示主題名稱(即在教師檔案上午8點顯示「語言」)。在數據庫中我有表 - 時間表,並有數據保存在DATETIME start_time和end_time。我有問題如何互連SQL和PHP,然後顯示。 我有這樣的SQL查詢:如何顯示來自數據庫的實際數據

SELECT lessons.schoolday, lessons.start_time, lessons.end_time, lectors.lectorsurname, studentgroups.class_id, classes.grade, subjects.subjectname 
    FROM lessons INNER JOIN lectors ON lessons.lector_id=lectors.lector_id 
    INNER JOIN studentgroups ON lessons.studentgroup_id=studentgroups.studentgroup_id 
    INNER JOIN classes ON studentgroups.class_id=classes.class_id 
    INNER JOIN subjects ON lessons.subject_id=subjects.subject_id 
    WHERE lessons.start_time <= substring(('".date('Y-m-d H:i:s')."'),11,6) 
    AND lessons.end_time >= substring(('".date('Y-m-d H:i:s')."'),11,6) 
    AND lectors.lector_id=:id"); 

當我改變日期,而不是時間間隔(FE start_time-> 0000-00-00 08:00:00,end_time-> 0000-00-00 8點45分00秒), 這是工作。但是通過這種方式,它僅打印Array()。 HTML與Latte模板一起顯示並且存在條件,如果爲空數組,則顯示message = Teacher當前沒有教導,但這也不起作用。

.......

PHP代碼:

 $stmt3 = $db->prepare(" SELECT l.schoolday, l.start_time, l.end_time, t.lectorsurname, sg.class_id, c.grade, s.subjectname 
           FROM lessons l 
           INNER JOIN lectors  t ON l.lector_id=t.lector_id 
           INNER JOIN studentgroups sg ON l.studentgroup_id=sg.studentgroup_id 
           INNER JOIN classes  c ON sg.class_id=c.class_id 
           INNER JOIN subjects  s ON l.subject_id=s.subject_id 
           WHERE l.start_time <= CURTIME() AND l.end_time >= CURTIME() AND t.lector_id=:id 
           ORDER By c.grade, l.schoolday, l.start_time"); 
     $stmt3->bindValue(":id", intval($_GET["id"])); 
     $stmt3->execute(); 
     $data=$stmt3->fetchAll(); 
     print_r($data); 

$tplVars["lesson"] = $stmt3->fetchAll(); 

和拿鐵模板

<table> 
    . 
    . 
    . 
    <tr> 
     <th> Aktuálne vyučuje: </th> 
    </tr> 
    {foreach $lesson as $aktual} 
    {if !empty($aktual['grade'])} 
    <tr> 
     <td>{$aktual['grade']} </td> 
    </tr> 
    {/if} 
    {if ($aktual['grade']) == NULL} 
    <tr> <td> <span> Učiteľ momentálne nevyučuje </span></td></tr> 
    {/if} 
    {/foreach} 
</table> 
+2

你有你的PHP代碼添加到這個問題對我們來說,與幫助。 – arkascha

+0

使用PHP mysqli查詢http://php.net/manual/en/mysqli.query.php – Ivan

回答

0

搜索的一些例子。一些別名的可讀性更好。 NOW()是不夠的?看到this link更多MySQL的日期和時間函數

SELECT l.schoolday, l.start_time, l.end_time, t.lectorsurname, sg.class_id, c.grade, s.subjectname 
FROM lessons l 
INNER JOIN lectors  t ON l.lector_id=t.lector_id 
INNER JOIN studentgroups sg ON l.studentgroup_id=sg.studentgroup_id 
INNER JOIN classes  c ON sg.class_id=c.class_id 
INNER JOIN subjects  s ON l.subject_id=s.subject_id 
WHERE l.start_time <= NOW() AND l.end_time >= NOW() AND t.lector_id=:id 
+0

實際上,似乎OP只需要datetime的時間部分,所以可能需要'CURTIME()'。 – Parfait

+0

我試着現在,curtime,它仍然顯示Array() – Sonya

+0

顯示您的PHP代碼,回聲 –

相關問題