简体   繁体   中英

Select rows inserted within 5 seconds ago

I'm trying to select rows from my database which were inserted within 5 seconds ago.

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 . 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

Then, note, that prepare does not execute the query, it only prepares it. 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() . 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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