'if语句'中的PHP time()没有传递给MySql查询?

时间:2011-02-16 13:32:53

标签: php mysql time if-statement

if($_SESSION['periode']){

    if($_SESSION['periode'] == 'Today'){
    $xdate = time() - (1 * 24 * 60 * 60);
    $ydate = time();
    }
    if($_SESSION['periode'] == 'Yesterday'){
    $xdate = time() - (2 * 24 * 60 * 60);
    $ydate = time() - (1 * 24 * 60 * 60);
    }
    if($_SESSION['periode'] == 'This week'){
    $xdate = time() - (7 * 24 * 60 * 60);
    $ydate = time() - (2 * 24 * 60 * 60);
    }
    if($_SESSION['periode'] == 'Older'){
    $xdate = 0;
    $ydate = time() - (7 * 24 * 60 * 60);
    }
    else{
    $xdate = 0;
    $ydate = time();
    }
}
else {
$xdate = 0;
$ydate = time();
}

$tweets = mysql_query("SELECT * FROM messages WHERE content LIKE '%$search%' 
AND content LIKE '%$region%' 
AND time BETWEEN $xdate AND $ydate ORDER BY id DESC LIMIT 10");

问题:所有消息都是从数据库中选择的,而不仅仅是$ xdate和$ ydate之间的消息。

如果我在else语句中手动填写unix时间段(并且没有定义$ _SESSION ['periode']),则从DB中挑选出正确的消息。发生什么事了?

1 个答案:

答案 0 :(得分:3)

if($_SESSION['periode'] == 'Older'){
    $xdate = 0;
    $ydate = time() - (7 * 24 * 60 * 60);
    }
else{
    $xdate = 0;
    $ydate = time();
    }

如果$ _SESSION ['periode']不是'较旧',此代码将始终将$ ydate设置为time()和$ xdate为0,因为此其他与上面的if相关。 你需要使用elseif而不是if

代码必须

if($_SESSION['periode']){

    if($_SESSION['periode'] == 'Today'){
    $xdate = time() - (1 * 24 * 60 * 60);
    $ydate = time();
    }
    elseif($_SESSION['periode'] == 'Yesterday'){
    $xdate = time() - (2 * 24 * 60 * 60);
    $ydate = time() - (1 * 24 * 60 * 60);
    }
    elseif($_SESSION['periode'] == 'This week'){
    $xdate = time() - (7 * 24 * 60 * 60);
    $ydate = time() - (2 * 24 * 60 * 60);
    }
    elseif($_SESSION['periode'] == 'Older'){
    $xdate = 0;
    $ydate = time() - (7 * 24 * 60 * 60);
    }
    else{
    $xdate = 0;
    $ydate = time();
    }
}
else {
$xdate = 0;
$ydate = time();
}