Php 如何一次运行一个查询?

Php 如何一次运行一个查询?,php,mysql,Php,Mysql,我制作了一个php页面,我希望如果数据发布然后查询为此运行我使用isset函数但对我不起作用它总是运行三个查询,但我希望一次运行一个查询我如何做到这一点?以下是我的代码: if(isset($_POST['fromdate'])){ $fromdate=date('Y-m-d h:i:s',strtotime($_POST['fromdate'])); echo $select="select b.email AS Email from buyer

我制作了一个php页面,我希望如果数据发布然后查询为此运行我使用isset函数但对我不起作用它总是运行三个查询,但我希望一次运行一个查询我如何做到这一点?以下是我的代码:

if(isset($_POST['fromdate'])){
                $fromdate=date('Y-m-d h:i:s',strtotime($_POST['fromdate']));
        echo $select="select b.email AS Email from buyers b,registrations r where b.buyer_id=r.buyer_id and r.event_id='".$_POST['events'][$i]."' and b.created_on>='".$fromdate."' group by r.buyer_id";
 $res = $GLOBALS ['mysqli']->query ($select) or die ($GLOBALS ['mysqli']->error . __LINE__);
$record=mysqli_num_rows($res);
    $recepients = $recepients + $record;                    
}
if(isset($_POST['todate'])){
$todate=date('Y-m-d h:i:s',strtotime($_POST['todate']));
echo $select1="select b.email AS Email from buyers b,registrations r where b.buyer_id=r.buyer_id and r.event_id='".$_POST['events'][$i]."' and b.created_on>='".$todate."' group by r.buyer_id";
$res1 = $GLOBALS ['mysqli']->query ($select1) or die ($GLOBALS ['mysqli']->error . __LINE__);
                $record1=mysqli_num_rows($res1);
                $recepients = $recepients + $record1;
            }
            if(isset($_POST['fromdate']) && isset($_POST['todate'])){ 
            $fromdate=date('Y-m-d h:i:s',strtotime($_POST['fromdate']));
            $todate=date('Y-m-d h:i:s',strtotime($_POST['todate'])); 
                echo $select2="select b.email AS Email from buyers b,registrations r where b.buyer_id=r.buyer_id and r.event_id='".$_POST['events'][$i]."' and b.created_on BETWEEN '".$fromdate."' AND  '".$todate."' group by r.buyer_id";
$res2 = $GLOBALS ['mysqli']->query ($select2) or die ($GLOBALS ['mysqli']->error . __LINE__);
                $record2=mysqli_num_rows($res2);
                $recepients = $recepients + $record2;
            }
这是我的阵列:

Array ( [events] => Array ( [0] => 7 ) [fromdate] => [todate] => February 11, 2014 2:55 PM [description] => [subject] => [fromname] => [replyto] => [senddatetime] => [timezone] => America/Los_Angeles [message] => [submit_skip] => Continue )
当我显示查询时,会给出错误的结果,这是我的查询

 select b.email AS Email from buyers b,registrations r where b.buyer_id=r.buyer_id and r.event_id='7' and b.created_on>='1970-01-01 12:00:00' group by r.buyer_id
 select b.email AS Email from buyers b,registrations r where b.buyer_id=r.buyer_id and r.event_id='7' and b.created_on>='2014-02-11 02:55:00' group by r.buyer_id
 select b.email AS Email from buyers b,registrations r where b.buyer_id=r.buyer_id and r.event_id='7' and b.created_on BETWEEN '1970-01-01 12:00:00' AND '2014-02-11 02:55:00' group by r.buyer_id 
你可以用!例如,在第一个上设置($\u POST['todate']

if(isset($_POST['fromdate']) && !isset($_POST['todate']){
    // code
}
if(isset($_POST['todate']) && !isset($_POST['fromdate']){
    // code
} 
if(isset($_POST['fromdate']) && isset($_POST['todate'])){ 
    // code
}

在您的数组中既有
fromdate
索引,也有
todate
索引。这就是执行3个代码块的原因。您只需检查索引,还需要检查值。即,不为null。还可以使用
if..else
。在这里,您可以使用以下命令:

//if both fromdate and todate are not null
if(isset($_POST['fromdate']) and $_POST['fromdate']!='' and isset($_POST['todate']) and $_POST['todate']!=''){
   //code
}
//if fromdate is not null
else if(isset($_POST['fromdate']) and $_POST['fromdate']!=''){
    //code
}
//if todate is not null
else if(isset($_POST['todate']) and $_POST['todate']!=''){
    //code
}

在if、elseif、else结构中使用此项同样,您的代码非常密集—请考虑将其显著分解—此外,使用$GLOBALS是非常不寻常的,并且在SQL=中直接使用$\u POST变量会遭到攻击。