如何让查询显示两个结果?

时间:2013-02-11 22:27:08

标签: php mysql

这是我的查询声明

$moc_query = $database->database_query("SELECT gg_congress_members.id,
  gg_congress_members.member_fname,gg_congress_members.member_lname,
  gg_congress_members.member_state,gg_congress_members.member_party,
  gg_congress_members.member_title
  FROM
    gg_congress_members
  WHERE (member_title = 'Sen' AND member_state='$user_state')
        OR (member_title = 'Representative' AND
            member_district = '$user_district'
            AND member_state='$user_state') ")
or die(mysql_error());

我的代码:

if ($user_info["user_id"])
{
    $senators .= "<h2>Your Senators are</h2>";
    $senators .= "<ol>";

    $reps .= "<h2>Your Congressperson is</h2>";
    $reps .= "<ol>";

    $rep_count = 0;
    foreach ($mocs as $moc)
    {
        print_r ($mocs);
        $moc_title = $moc['moc_title'];
        $moc_fname = $moc['moc_fname'];
        $moc_lname = $moc['moc_lname'];
        $moc_party = $moc['moc_party'];
        $moc_state = $moc['moc_state'];

        if ($moc['moc_title'] == "Sen")
        {
            $senators .= "<li>$moc_fname $moc_lname - $moc_party ($moc_state)</li> ";           
        }

        if ($moc['moc_title'] == "Representative" )
        {
            $rep_count++;
            $reps .= "<li>$moc_fname $moc_lname - $moc_party ($moc_state)</li> ";
        }

    }
}

它只显示不是参议员的代表。如果我删除了有关查询中代表的部分,那么它会显示参议员。

1 个答案:

答案 0 :(得分:0)

您如何绑定参数?我想知道两次使用相同名称是否会让某些事情变得混乱(尽管它不应该)。如果移除代表显示参议员,那么这样的事情可能会起作用:

SELECT gg_congress_members.id,
   gg_congress_members.member_fname,
   gg_congress_members.member_lname,
   gg_congress_members.member_state,
   gg_congress_members.member_party, 
   gg_congress_members.member_title  
FROM gg_congress_members  
WHERE member_state='$user_state' AND 
   ((member_title = 'Sen') OR 
   (member_title = 'Representative' AND member_district = '$user_district'))
祝你好运。