如何显示DB的实际数据

时间:2015-12-25 22:30:05

标签: php mysql sql

我正在做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");

当我改变日期而不是间隔(f.e.start_time-&gt; 0000-00-00 08:00:00,end_time-&gt; 0000-00-00 08: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>

1 个答案:

答案 0 :(得分:0)

搜索一些示例。有些别名,它的可读性更好。 NOW()还不够吗?有关更多MySql日期和时间函数,请参阅this link

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