dongpo1203 2013-10-21 22:15
浏览 56
已采纳

php mysql动态选择用户输入的查询

I have a form with checkboxes, input fields. The user can select or enter something and with that a query is being build. Now when the user selects only one checkbox, the query works perfect, but when selecting 2 checkboxes, I get a count of zero in return. This is what the query returns:

SELECT nbs_contact_events.contact_id, nbs_contacts.nbs_contact_id, nbs_contacts.nbs_contact_sur
FROM nbs_contacts
LEFT JOIN nbs_contact_events ON nbs_contact_events.contact_id = nbs_contacts.nbs_contact_id
WHERE nbs_contact_events.event_id = 1
  AND nbs_contact_events.event_id = 21

returned count: 0

The php (pdo) code for making that selection is as followed:

$this->pdo = $this->connectMySql();
        $query = "SELECT nbs_contact_events.contact_id, nbs_contacts.nbs_contact_id, nbs_contacts.nbs_contact_sur 
                      FROM 
                        nbs_contacts 
                      LEFT JOIN 
                        nbs_contact_events 
                      ON 
                        nbs_contact_events.contact_id = nbs_contacts.nbs_contact_id";
        if(isset($eventlist) || isset($nbslist) || isset($nbs_relativeto)){
            $query .= " WHERE ";

            if(isset($eventlist)){                  
                $source = array_filter($eventlist);
                $conditions = array();
                foreach($source as $field => $value){

                    $conditions[] = "nbs_contact_events.event_id = " . mysql_real_escape_string($value);

                    //$conditions[] = "event_id = :event_id";
                }
                $where = implode(" AND ", $conditions);
                $query .= $where;
            }
        }
        $stmt = $this->pdo->prepare($query);
        print_r($query);
        if(!$stmt->execute($conditions)){
            return false;
        }

        $nart = $stmt->rowCount();
        echo "returned count: " . $nart;
        if ($nart == 0) {
            return false;
        }

What is wrong with the query, why isn't it returning anything? I know the ID 1 and 21 are in the database (if I select them separately, it works).

Thanks

  • 写回答

1条回答 默认 最新

  • douhun7609 2013-10-21 22:19
    关注

    It should be OR instead of AND. AND would only return something if event_id was both 1 AND 2. OR returns something if event_id is 1 OR event_id is 2.

    select nbs_contact_events.contact_id,
      nbs_contacts.nbs_contact_id,
      nbs_contacts.nbs_contact_sur
    from nbs_contacts
    left join nbs_contact_events
      on nbs_contact_events.contact_id = nbs_contacts.nbs_contact_id
    where nbs_contact_events.event_id = 1
      or nbs_contact_events.event_id = 21
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ImportError: DLL load failed while importing _iterative: 找不到指定的模块。
  • ¥15 控制工程研究生方向选择
  • ¥15 如何通过交互分析得出某高危患者对放疗获益更多
  • ¥15 相关性分析中,p<0.05, r=0.29,怎么评价相关性呢
  • ¥15 docker部署Mongodb后输入命令报错?
  • ¥15 将下列流程图转变成python程序代码
  • ¥15 我需要全国每个城市的最新小区名字等数据。
  • ¥15 开发一个小区生态的小程序
  • ¥15 如何解决Excel中dependent dropdown list 的问题
  • ¥15 MddBootstrapInitialize2失败