if else条件检查3个语句

时间:2014-06-06 09:10:12

标签: php if-statement

这是我检查id的代码。我想检查sesion id是1还是2。如果它是1,那么相应的查询从db获取数据并执行。但是这里我得到的查询是空的。

<?php
if($_SESSION['id'] =='1')
{
$sql ="select a.`id` 'User ID', a.`username` 'Username',MAX(b.login_timestamp) 'Last Logged',count(*) 'Total No. of logins for the Month', count(*)/30 'Avg no. of Logins /day'  from users a left join `client_access_log` b  on b.unique_id=a.unique_id  where b.`username` in( 'ghfh','sdd')  and 
`login_timestamp` between DATE_SUB(DATE_FORMAT(NOW(),'%Y-%m-01'), INTERVAL 1 MONTH) and CAST(DATE_FORMAT(NOW(),'%Y-%m-01') AS DATE) GROUP BY a.id order by count(*) desc";
}
elseif($_SESSION['id'] == '2')
{
$sql ="select a.`id` 'User ID', a.`username` 'Username',MAX(b.login_timestamp) 'Last Logged',count(*) 'Total No. of logins for the Month', count(*)/30 'Avg no. of Logins /day'  from users a left join `client_access_log` b  on b.unique_id=a.unique_id  where b.`username` in( 'dff')  and 
`login_timestamp` between DATE_SUB(DATE_FORMAT(NOW(),'%Y-%m-01'), INTERVAL 1 MONTH) and CAST(DATE_FORMAT(NOW(),'%Y-%m-01') AS DATE) GROUP BY a.id order by count(*) desc";
}
elseif($_SESSION['id'] == '3')
{
$sql ="select a.`id` 'User ID', a.`username` 'Username',MAX(b.login_timestamp) 'Last Logged',count(*) 'Total No. of logins for the Month', count(*)/30 'Avg no. of Logins /day'  from users a left join `client_access_log` b  on b.unique_id=a.unique_id  where b.`username` in( 'wededf')  and 
`login_timestamp` between DATE_SUB(DATE_FORMAT(NOW(),'%Y-%m-01'), INTERVAL 1 MONTH) and CAST(DATE_FORMAT(NOW(),'%Y-%m-01') AS DATE) GROUP BY a.id order by count(*) desc";
}
$query = mysql_query($sql) or die(mysql_error());
    if(!$query)
    {
        mysql_close();
        echo json_encode("There was an error running the query: " . mysql_error());
    }
    elseif(mysql_num_rows($query)==0)
    {
        mysql_close();
        echo json_encode("No results returned");
        exit;
    }
    else
    {
        $header = false;
        $output_string = "";
        $output_string .=  "<table border='2'>\n";
        while($row = mysql_fetch_assoc($query))
        {
            if(!$header)
            {
                $output_string .= "<tr>\n";
                foreach($row as $header => $value)
                {
                    $output_string .= "<th>{$header}</th>\n";
                }
                $output_string .= "</tr>\n";
            }
            $output_string .= "<tr>\n";
            foreach($row as $value)
            {
                $output_string .= "<th>{$value}</th>\n";
            }
            $output_string .= "</tr>\n";
        }
        $output_string .= "</table>\n";
    }
    mysql_close();
    echo json_encode($output_string);
        ?>

1 个答案:

答案 0 :(得分:0)

嘿,试着删除&#39;&#39;从比较

<?php
session_start();
if ($_SESSION['id'] == 1)
{
$sql ="select a.`id` 'User ID', a.`username` 'Username',MAX(b.login_timestamp) 'Last Logged',count(*) 'Total No. of logins for the Month', count(*)/30 'Avg no. of Logins /day'  from users a left join `client_access_log` b  on b.unique_id=a.unique_id  where b.`username` in( 'ghfh','sdd')  and 
`login_timestamp` between DATE_SUB(DATE_FORMAT(NOW(),'%Y-%m-01'), INTERVAL 1 MONTH) and CAST(DATE_FORMAT(NOW(),'%Y-%m-01') AS DATE) GROUP BY a.id order by count(*) desc";
}
elseif ($_SESSION['id'] == 2)
{
$sql ="select a.`id` 'User ID', a.`username` 'Username',MAX(b.login_timestamp) 'Last Logged',count(*) 'Total No. of logins for the Month', count(*)/30 'Avg no. of Logins /day'  from users a left join `client_access_log` b  on b.unique_id=a.unique_id  where b.`username` in( 'dff')  and 
`login_timestamp` between DATE_SUB(DATE_FORMAT(NOW(),'%Y-%m-01'), INTERVAL 1 MONTH) and CAST(DATE_FORMAT(NOW(),'%Y-%m-01') AS DATE) GROUP BY a.id order by count(*) desc";
}
elseif ($_SESSION['id'] == 3)
{
$sql ="select a.`id` 'User ID', a.`username` 'Username',MAX(b.login_timestamp) 'Last Logged',count(*) 'Total No. of logins for the Month', count(*)/30 'Avg no. of Logins /day'  from users a left join `client_access_log` b  on b.unique_id=a.unique_id  where b.`username` in( 'wededf')  and 
`login_timestamp` between DATE_SUB(DATE_FORMAT(NOW(),'%Y-%m-01'), INTERVAL 1 MONTH) and CAST(DATE_FORMAT(NOW(),'%Y-%m-01') AS DATE) GROUP BY a.id order by count(*) desc";
}

并尝试在关键字之间留出一些空格。