我在MySQL中有21个字段的数据库,下面列出
Field Name Data Type NULL
status text No
roll_no text No
branch_id int(5) No
student_name text No
father_name text No
phone1 text No
phone2 text No
email text No
dob date No
city text No
course_id varchar(5) No
class_id int(2) No
program text No
duration text No
comment text No
admission_year int(4) No
admission_date text No
entryby text No
address text No
admission_no int(4) No
fees int(6) No
现在我被困在搜索过程中,我有点困惑,我怎么能执行搜索不同类型的条件/标准
几个条件组合的例子
- 只记录城市=3
- branch_id=2
- 只允许那些admission_year='2013'
- 只有那些course_id='15'
- 仅限branch_id='2' AND course_id='15'
- 只有branch_id='2' AND course_id='15' AND city LIKE 'XYZ'
- 只有那些admission_year='2012' AND course_id='10'且持续时间在(2和3)之间的
- 只有branch_id=2 AND course_id='15' AND student_name LIKE 'XYZ'
- 只有那些course_id=7 AND class_id=2 AND father_name LIKE 'XYZ'
我的search.php表单页面是设计的,我上传了表单设计的图像在这里,但我很困惑,我怎么能实现这个搜索选项的不同情况。
请给我一些指导,告诉我解决这个问题的正确方法。
这就是简单的SQL:
SELECT * FROM mytable WHERE city="3" AND branch_id=2 AND admission_year=2013 ...
注意int
类型的所有字段不能有引号(" ")。
这很简单。你可以这样做:
$search = "";
if(isset($records_of_city) && $records_of_city != "")
$search .= "records_of_city = 3 AND";
if(isset($branch_id) && $branch_id != "")
$search .= "branch_id = 3 AND";
if(isset($admission_year) && $admission_year != "")
$search .= "admission_year = 3 AND";
// Continue till all search criteria are appended to $search
// And at the end
$query = "SELECT * from tableName where $serach active_status = 'active'"
在查询中,只有那些具有某些值的条件才会被附加,否则将被忽略。还有一件事是我们在每个IF条件之后添加AND操作符,因此您需要在查询结束时添加一个条件(您可以设置条件用户状态,删除状态等),以便查询不会给出任何错误。