简体   繁体   English

选择5秒前插入的行

[英]Select rows inserted within 5 seconds ago

I'm trying to select rows from my database which were inserted within 5 seconds ago. 我正在尝试从数据库中选择5秒钟前插入的行。

I'm currently working with this query: 我目前正在使用此查询:

$timerightnow = date("Y-m-d H:i:s");

if($stmt = $con->prepare("SELECT notimessage,
                          Second(TIMEDIFF('$timerightnow',dateofreg)) AS Second1
                          FROM notitb
                          WHERE checked = 0 HAVING Second1 <= 5")){

But it doesn't show any results. 但是它没有显示任何结果。 Am I doing it correctly? 我做得对吗? Or my query is simply incorrect just from the beginning? 还是我的查询从一开始就是错误的?

Your query certainly has some syntax errors. 您的查询当然有一些语法错误。 In particular, your $timerightnow is a string, but it is not in the quotes, ans Second1 is an alias, and therefore cannot be used in WHERE , it can only be used in HAVING . 特别是,$ timerightnow是一个字符串,但不在引号中,ans Second1是一个别名,因此不能在WHERE ,只能在HAVING It would help you if you can see the errors, instead of trying to debug them blindly. 如果您可以看到错误,则将对您有所帮助,而不是尝试盲目地调试它们。 This will help: 这将有助于:

http://php.net/manual/en/mysqli.error.php http://php.net/manual/en/mysqli.error.php

Then, note, that prepare does not execute the query, it only prepares it. 然后,请注意, prepare不执行查询,而只是准备查询。 You then need to bind the parameters (in particular, I would bind the current date rather than hardcode it into the query string), and then you need to execute the query via $stmt->execute() . 然后,您需要绑定参数(特别是,我将绑定当前日期,而不是将其硬编码到查询字符串中),然后您需要通过$stmt->execute()执行查询。 It is not clear if you are doing that, because your code is truncated, if you are not, then this will be helpful: 目前尚不清楚是否要这样做,因为代码会被截断,否则,代码将被截断:

http://php.net/manual/en/mysqli.prepare.php http://php.net/manual/en/mysqli.prepare.php

See the example there where they prepare a query, bind some params, and execute it. 请参阅那里的示例,在该示例中他们准备查询,绑定一些参数并执行它。

Finally, you might want your predicate be Second1 <= 5 instead of Second1 = 5 , if you want to get everything from the last 5 seconds, not just the things that happened during the second that was five seconds ago. 最后,如果您想获得最近5秒钟内的所有内容,而不仅仅是五秒钟前第二秒钟发生的事情,则可能希望谓词为Second1 <= 5而不是Second1 = 5

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

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