Hello

I have databse in MySQL with 21 fields which are listed below

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

Now I am stuck with search process, I bit confused, how can I perform search for different types of conditions/criteria

Few Examples of combinations of condtions
1. Only those records of city=3
2. Only those branch_id=2
3. Only those admission_year='2013'
4. Only those course_id='15'
5. Only those branch_id='2' AND course_id='15'
6. Only those branch_id='2' AND course_id='15' AND city LIKE 'XYZ'
7. Only those admission_year='2012' AND course_id='10' AND duration BETWEEN(2 AND 3)
8. Only those branch_id=2 AND course_id='15' AND student_name LIKE 'XYZ'
8. Only those course_id=7 AND class_id=2 AND father_name LIKE 'XYZ'

My search.php form page is designed, I uploaded the image of form design here but I am confused how can I implement this search options for different situations.

Please give me some guidance and show me the correct way to solve this issue.

Create a function
like

function mySearchQuery($city='',$branch_id='',$admission_year=''){//fields you want to search

    if($city!=""){
        $city = "AND city ='".$city."'";
    }else{
        $city = "";
    }


    $myQuery = "select table fields from table where ".$city.$branch_id;
    //fetch array ...
}

and call this function where you want by passing the parameters

like
mySearchQuery('cityName','branch_id') //and so on

commented: ALWAYS escape SQL strings ($city). ALWAYS! (sorry, but I downvote SQL injection prone code by default) +0

@Kamran Thank you for your consideration. So you want to say I shall check for each field using php if condition as you shown in your code and concatanate them in $myQuery.

he way outtYes, bcoz u never know which user wants what, so you have to check every field on which u want to enable search, W this is only example @Angry whats so ever. not giving complete code just giving

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.