php - 在 zend 框架中构建搜索查询

标签 php mysql zend-framework

我正在使用 ZendFrameWork 1.11.5 我需要进行多表单搜索查询,就像我有 5 个字段一样

name (text box)
city (dropDown)
zipCode (text box)
type (dropdown)

现在的问题是... 在文本框中,用户可以输入任何内容,可能是正确的信息,也可能是错误的信息。 请建议我如何构建快速查询。我有什么样的选择.. 我也尝试过这个但不起作用..没有给我正确的结果..

Select * from table where type =       '%$pType%'
                OR  sex     LIKE     '%$sex%'
                OR  race    LIKE      '%$race%'
                OR  kind    LIKE     '%$kind%'
                OR  country LIKE      '%$Country%'
                OR  state   LIKE     '%$statesIDs%'
                OR  zipcode LIKE     '%$zip%'";

最佳答案

这是我为您提供的示例代码。您的代码应该取决于您的需求,查询可能会与另一个表连接,或者您可以使用 MATCH...AGAINST 代替 LIKE 以获得更准确的结果。此代码取决于用户输入的用于进行最终查询的每个参数。

//In DbTable
public function search($params)
{
    $query = $this->select()
                   ->from(
                    array('tbl'=>'table'),
                    array('name','city','zipcode','type')
                    );
    $query = $this->_makeParams($query,$params);
    return $this->fetchAll($query);
}

private function _makeParams($query, $params)
{
    $name = isset($params['name']) ? trim($params['name']) : '';
    $city = isset($params['city']) ? trim($params['city']) : '';
    $zipcode = isset($params['zipcode']) ? trim($params['zipcode']) : '';
    $type = isset($params['type']) ? trim($params['type']) : '';

    if($name!='')
    {
        $name = '%'.$this->quote($name).'%';//quote is my own function
        $query->where("tbl.name LIKE '?'",$name); 
    }

    if($city!='')
    {
        $query->where("tbl.city=?",$city);
    }

    if($zipcode!='')
    {
        $query->where("tbl.zipcode=?",$zipcode);
    }

    if($type!='')
    {
        $query->where("tbl.type=?",$type);
    }

    return $query;

}

关于php - 在 zend 框架中构建搜索查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8584296/

相关文章:

php - Apache 中的 PHP Curl 库出现 SSL 错误

php - 循环日期函数php

mysql - 选择mysql数据并根据时间戳列设置变量值

mysql - 无法解析查询,请检查查询的语法。 (ORA-00905 : missing keyword)

php - Zend Framework 中的干净 Ajax Controller 实现

php - 如何将从 SQL 数据库表中提取的时间戳显示为用户时区?

php - 使用 PHP 和 Symfony 的 Websockets - 网络和服务器架构

mysql - SQL 效率 - MySQL Case/sub 选择

php - Zend 框架中模块的用例

css - Zend Framework 中的样式表单元素使用默认样式