Reputation: 5991
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?
Upvotes: 0
Views: 222
Reputation: 12795
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.
Upvotes: 1