MySQL datetime versus datetime set in session. Which came first?
I'm trying to calculate whether the results i get from the database are made after the time set in session. However, it doesn't seem to work.
I set the time session like this:
$_SESSION['lastVisit'] = $user['lastVisit'];
Where $user['lastVisit'] is a datetime field in my database.
And i'm trying to select results from the database doing this:
if($unreadThreadsSql = $cms->db->query('SELECT * FROM forum_threads WHERE posted < ' . $_SESSION['lastVisit']))
I'm not sure what i'm doing wrong here, and would like your guys opinion. For reference, here's the entire code. I might just be doing something wrong somewhere.
public function __construct()
{
$this->getUnread();
}
private function getUnread()
{
global $cms;
if($unreadThreadsSql = $cms->db->query('SELECT * FROM forum_threads WHERE posted < ' . $_SESSION['lastVisit']))
{
$forumSQL = $cms->db->query('SELECT * FROM forum_for');
while(开发者_JAVA百科$forums = $forumSQL->fetch_assoc())
{
$forumId = $forums['id'];
$forumArray[$forumId] = $forums;
}
while($unreadThreads = $unreadThreadSql->fetch_assoc())
{
$forumId = $unreadThreads['forId'];
$cms->db->query('INSERT INTO forum_unread(threadID, catID, forumID, userID, datetime, threadtime) VALUES('.$unreadThreads['id'].', '.$forumArray[$forumId]['cat_id'].','.$forumId.','.$_SESSION['userId'].',NOW(),'.$unreadThreads['posted'].' )');
}
}
}
Help is appreciated guys.
What is the value of $_SESSION['lastVisit']
? And shouldn't you be quoting the value, like:
"SELECT * FROM forum_threads WHERE posted > '" . $_SESSION['lastVisit'] . "'"
SELECT * FROM forum_threads WHERE posted < ' . $_SESSION['lastVisit']
Shouldn't that be > ' . $_SESSION['lastVisit']
?
精彩评论