PHP Mysql搜索查询 [英] PHP Mysql Search Query

查看:42
本文介绍了PHP Mysql搜索查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有一个简单的搜索查询,我所面对的是,当某人写下他要搜索的用户的唯一名字时,我的查询找到了它,也有人在输入中仅写下了姓氏,并且发布它,它也显示了这一点,但是当用户在输入中同时输入名字和姓氏时,即使他/她存在,也找不到该用户. $ q查询的最后一部分,我写的名字和姓氏这样的部分不起作用,我知道我的逻辑不好,但是我该怎么解决

Hello i have a simple search query, what i'm facing is when someone writes the only first name of the user that he wants to search, my query finds it, also when someone only writes the last name in the input and posts it, it also shows that too, but when user writes first name and last name together in the input, it can't find the user even he/she exists. The last part of $q query where i wrote first name and last name like part doesnt work i know there my logic is bad, but how can i fix that

    try {
        $q = "SELECT * FROM `members` WHERE `first_name` LIKE :search_string OR `last_name` LIKE :search_string OR `first_name` AND `last_name` LIKE :search_string";
        $q_do = $db->prepare($q);
        $q_do->execute( array("search_string"=>'%'.$query.'%') );
        $number = $db->query("SELECT FOUND_ROWS()")->fetchColumn();
    } catch(PDOException $e) {
        $log->logError($e." - ".basename(__FILE__));
    }

谢谢

推荐答案

尝试使用concat:

Try using concat:

$q = "SELECT * FROM `members` WHERE `first_name` LIKE :search_string 
OR `last_name` LIKE     :search_string 
OR concat(`first_name` , ' ', `last_name`) LIKE :search_string";

这篇关于PHP Mysql搜索查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆