I have a query that I am using to get results that are newer than a given timestamp. It is supposed to find all those newer messages that were sent and received by a given user. The problem is, no matter what I change the timestamp parameter to, I even changed it to 'g', I still get 4 results. The timestamp doesn't seem to alter my query. In the table, the timestamp is an int(11)
. I tried using intval and defining the bindvalue as a param int and still no luck. Any ideas?
$sql = 'SELECT timestamp, user, message, receiver, convo_id
FROM chatbox
WHERE user = ?
OR receiver = ?
AND timestamp = ?
ORDER BY timestamp DESC';
$stmt = $conn->prepare($sql);
$stmt->bindValue(1,'username');
$stmt->bindValue(2,'username');
$stmt->bindValue(3,'g');
$stmt->execute();
I suspect it's the precedence of AND and OR in your where clause. Try using parentheses:
WHERE ( user = ? OR receiver = ? )
AND timestamp = ?
assuming that's what you need.
Shouldn't you do this instead in your $sql variable?
AND timestamp > ?