mysql 和或一起查询 [英] mysql and or query together

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

问题描述

我需要制作一个搜索引擎,用户可以通过姓名、年份、成员、年份(文本字段)进行搜索

I need to make a search engine where a user can search by name,year,member,year(text field)

搜索将包含任何一个字段或者搜索将与所有领域或者搜索将包含多个字段- 怎么可能只使用一个查询??现在我的代码是

search will be with any one field or search will be with all the field or search will be with more than one field -How it is possible by using only one query?? now my code is

$query="select * from fsb_profile where profile_name = '".$_REQUEST['name']."' and 
    profile_member= '".$_REQUEST['type']."' and profile_year= '".$_REQUEST['year']."'  
    and profile_course='".$_REQUEST['course']."' or profile_name = '".$_REQUEST['name']."'  
    or profile_member= '".$_REQUEST['type']."' or profile_year= '".$_REQUEST['year']."'  
    or profile_course='".$_REQUEST['course']."'";

-但它不起作用?

推荐答案

你可以这样使用:

SELECT ...
FROM ...
WHERE (profile_name = '$profile_name' OR '$profile_name' = '')
  AND (profile_member = '$profile_member' OR '$profile_member' = '')
  AND ...

或者,您可以根据您拥有的字段动态构建查询.

Alternatively you could build the query dynamically based on the fields you have.

无论哪种方式,您都应该避免将用户输入中的数据直接放入查询中而不先将其转义.看看mysql_real_escape_string 或使用绑定参数.

Either way you should avoid putting data directly from user input into a query without first escaping it. Take a look at mysql_real_escape_string or use bind parameters.

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

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