简体   繁体   English

根据当前时间从数据库中获取数据

[英]Fetching data from database based on current time PHP

So I am fetching data from my PostgreSQL data by executing a query in my Index.php. 因此,我通过在Index.php中执行查询来从PostgreSQL数据中获取数据。 This query fetches the name of the bus station name along with the corresponding time it will be arriving. 该查询将获取公交车站名称的名称以及它将到达的相应时间。 What I want my application to do is fetch times for a specific bus station selected based on the current time. 我希望我的应用程序执行的操作是基于当前时间选择的特定公交车站的提取时间。

My query is the following: 我的查询如下:

   $query = " SELECT busname, bustime
              From busses
             where name = '$name' and time ='$time' LIMIT 5";

Would I need to do something in my query to achieve this or in my database ? 我是否需要在查询中执行某些操作以实现此目的或在数据库中进行?

If you are basing it of current time you definitely don't want time='$time' as this will only ring true if you happen to be querying at the exact time down to the millisecond. 如果您以当前时间为基础,则绝对不希望time ='$ time',因为只有当您恰好在精确到毫秒的时间查询时,这才是正确的。

Presuming you are wanting to get all (or closest) times for a bus station based on current time then try: 假设您希望基于当前时间获取公交车站的所有(或最近的)时间,然后尝试:

$query = " SELECT busname, bustime
           From busses
           WHERE name = '$name' 
           AND time >= '$time'  
           ORDER BY time ASC 
           LIMIT 5";

This will give you all times after current time and then ordering by ASC will give the nearest time first, limiting 5 then returns only the next 5 bus times. 这将为您提供当前时间之后的所有时间,然后通过ASC进行订购将首先给出最接近的时间,限制为5,然后仅返回接下来的5个公交时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM