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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?