Php搜索多个单词

时间:2017-09-23 16:06:27

标签: php mysql wordpress

所以,我已经定义了一个从DB搜索并将结果传递给ajax的函数。

function do_search() {
    $search=$_POST['dname'];
    global $wpdb;
    $sql="SELECT * FROM employee WHERE `firstname` LIKE '%{$search}%' OR `lastname` LIKE '%{$search}%' OR  `department`LIKE '%{$search}%' OR `phone` LIKE '%{$search}%' OR `job_title` LIKE '%{$search}%' OR `cell_phone` LIKE '%{$search}%' OR `image`LIKE '%{$search}%' OR `email` LIKE '%{$search}%' OR `address` LIKE '%{$search}%' OR `room` LIKE '%{$search}%';";
    $result2=$wpdb->get_results($sql);
    echo json_encode($result2);
    wp_die();
}

但我的问题是它只用一个词来工作。我尝试用“”爆炸$ _POST值。然后制作foreach并构建多个查询,比如$ query [i]但我无法得到回应?

如何在回显结果时实现 - 那么它会回应每个单词与那些字段条目进行比较的结果?

1 个答案:

答案 0 :(得分:1)

您可以使用简单的“技巧”:

,而不是爆炸搜索查询并产生大量条件
      $em = $this->getServiceLocator()-> get('Doctrine\ORM\EntityManager');
            $repo = $em->getRepository('Login\Entity\Doctor');
            $queryBuilder = $repo->createQueryBuilder('d');
            $queryBuilder->select('d');
            $queryBuilder->where($queryBuilder->expr()->eq('firstname', 
             ':firstname')->setParameter('firstname', $firstname))
                         ->andwhere($queryBuilder->expr()->eq('lastname', 
             ':lastname')->setParameter('lastname', $lastname))
                         ->andwhere($queryBuilder->expr()->eq('city', 
             ':city')->setParameter('city', $city))
                         ->andwhere($queryBuilder->expr()->eq('speciality', 
             ':speciality')->setParameter('speciality', $speciality));
            $queryBuilder>getQuery()>setHydrationMode(Query::HYDRATE_ARRAY);
            $result = $queryBuilder->getQuery()->getResult();
            return new JsonModel($result);

不要忘记转义输入或参数化查询。