如何检查查询的数据是否包含任何记录

时间:2014-10-29 05:50:47

标签: php mysql zend-framework

我的代码如下:

 $order = $this->getTableGateway('order');

        $select = new Select();

        if($txtFromOrderDate!="" && $txtToOrderDate=="")
        {
        $select->from('order')
            ->join('user', 'order.user_id = user.id', array('email'))
            ->where ("order.created between ".$txtFromOrderDate." and (
                        SELECT MAX(  `created` ) 
                        FROM  `order`
                        )"
                     );
        }

        else if($txtFromOrderDate=="" && $txtToOrderDate!="")
        {
         $select->from('order')
            ->join('user', 'order.user_id = user.id', array('email'))
            ->where ("order.created between  (
                        SELECT MIN(  `created` ) 
                        FROM  `order`
                        ) and ".$txtToOrderDate
                     );
        }


        else if($txtFromOrderDate!="" && $txtToOrderDate!="")
        {
        $select->from('order')
            ->join('user', 'order.user_id = user.id', array('email'))
            ->where ("order.created between ".$txtFromOrderDate." and ".$txtToOrderDate);
             //->where ('order.created between '.$txtFromOrderDate.'  and '.$txtToOrderDate);
        }

        else if($txtFromOrderDate=="" && $txtToOrderDate=="")
        {
        $select->from('order')
            ->join('user', 'order.user_id = user.id', array('email'));

        }   

        $data = $order->selectWith($select)->toArray();

正如我们可以看到,如果select语句正在改变,取决于不同的条件。

最后我正在执行它:

$data = $order->selectWith($select)->toArray();

我想检查查询是否已返回$ data中的任何记录。

如何检查查询是否返回0条记录????

请帮帮我。

我正在使用MySQL服务器和Zend Framework for PHP

3 个答案:

答案 0 :(得分:3)

$data将是array。如果没有记录那么

count($data)将返回0

答案 1 :(得分:3)

使用sizeof($ data)或count($ data)

答案 2 :(得分:2)

if(!empty($data)) {
   //query has returned 1 or more results
}
else {
   //query has returned 0 result
}