像查询mysql php中的多个值

时间:2014-05-12 11:38:10

标签: php mysql

我有这段代码:

<?php 
$agenti = $_GET['agenti'];
$agen = array();
if (is_array($agenti)) {foreach($agenti as $val) { $agen[] = "ch.employeename LIKE '" . $val . "'"; }} else {$agen[] = 'true';}

    $raw_results = mysql_query("SELECT distinct ch.employeename, ch.customername, 
                                       ch.customerowner, ch.QuestionnaireName, ch.parentgroupname,
                                       ch.groupname, ch.visitdate from chestionare ch
                                    WHERE ch.visitdate >= '".$_GET['from']."' AND ch.visitdate <= '".$_GET['to']."' 
                                    AND (" . implode(' OR ', $agen) . ")
                                    AND ch.customerowner like '$clienti' 
                                    AND ch.class like '$task' 
                                    AND ch.parentgroupname like '$categorie' 
                                    ") 
                                or die(mysql_error());

    if(mysql_num_rows($raw_results) > 0){ 
        while($results = mysql_fetch_array($raw_results)){ ?>

如果$agenti有多个值(60 values),我该怎么办?值来自另一个<select multiple="multiple"> <option value="test"> test </option>< /select>

的文件

3 个答案:

答案 0 :(得分:3)

如果$agenti是动态数组。简单的例子:

$agen = array();

foreach ($agenti as $val) {
    $agen[] = "ch.employeename like '" . $val . "'"; 
}

$raw_results = mysql_query(
           "SELECT distinct ch.employeename, ch.customername, 
               ch.customerowner, ch.QuestionnaireName, ch.parentgroupname,
               ch.groupname, ch.visitdate from chestionare ch
            WHERE ch.visitdate >= '".$_GET['from']."' AND ch.visitdate <= '".$_GET['to']."' 
            AND (" . implode(' OR ', $agen) . ")
            AND ch.customerowner like '$clienti' 
            AND ch.class like '$task' 
            AND ch.parentgroupname like '$categorie' 
         ");
or die(mysql_error());

答案 1 :(得分:1)

我认为您可以使用OR之类的

如果你的意思是$ agenti是一个数组,所以它可能是这样的:

$agenti=array(John,Tommy,Mark);

这意味着:

$agenti=array(
    0=>John,1=>Tommy,2=>Mark
);

所以你应该指向像$agenti[0]

这样的元素
$raw_results = mysql_query("SELECT distinct ch.employeename, ch.customername, ch.customerowner, ch.QuestionnaireName, ch.parentgroupname,
  ch.groupname, ch.visitdate from chestionare ch
   WHERE ch.visitdate >= '{$_GET['from']}' AND ch.visitdate <= '{$_GET['to']}' 
    AND (
         ch.employeename like '{$agenti[0]}' 
         OR ch.employeename like '{$agenti[1]}' 
         OR ch.employeename like '{$agenti[2]}' 
    )
    AND ch.customerowner like '{$clienti}' 
    AND ch.class like '{$task}' 
    AND ch.parentgroupname like '{$categorie}' 
 ") or die(mysql_error());

答案 2 :(得分:1)

<?php
            $agenti = array('sam','robin','sugar');
            $qry = "SELECT distinct ch.employeename, ch.customername, 
                     ch.customerowner, ch.QuestionnaireName, ch.parentgroupname,
                      ch.groupname, ch.visitdate from chestionare ch
                    WHERE ch.visitdate >= '".$_GET['from']."' AND ch.visitdate <= '".$_GET['to']."' 
                    AND ch.employeename IN('".implode("','",$agenti)."')  
                    AND ch.customerowner 
                    AND ch.class like '$task' 
                    AND ch.parentgroupname like '$categorie'
                                    ";

            /* Always use this way so it becomes easier for you to echo the query*/                         
            $raw_results = mysql_query($qry);
?>