Hi friends i have a custom search function in my website. Search is depending on two tables such as,
product_table
CREATE TABLE `product_table` (
`product_id` int(11) NOT NULL auto_increment,
`product_name` varchar(100) NOT NULL,
`country` varchar(200) NOT NULL,
`added_time` timestamp NOT NULL default CURRENT_TIMESTAMP,
PRIMARY KEY (`product_id`));
multiple_table
CREATE TABLE `multiple_table` (
`multi_id` int(11) NOT NULL auto_increment,
`cat_id` int(11) NOT NULL,
`product_id` int(11) NOT NULL,
PRIMARY KEY (`multi_id`));
I am giving option to users for search by one or more fields as product_name,country,cat_id,added_time
my search function as follows,
if(isset($_GET['s_button'])){
$sql2="SELECT a.product_id FROM product_table a, multiple_table b WHERE b.product_id= a.product_id ";
$where=array();
$values = array();
$types = '';
if(!empty($_GET['search_cat'])){
$search_cat=clean($_GET['search_cat']);
$where[]="AND cat_id='".$search_cat."'";
}
if(!empty($_GET['product_country'])){
$country=clean($_GET['product_country']);
$where[]="AND country='".$country."'";
}
if(!empty($_GET['model'])){
$keyword=clean($_GET['model']);
$where[]="AND product_name LIKE '%".$keyword."%'";
}
$sql2 .= implode($where). " ORDER BY a.product_id ";
}else{
$sql2 = "SELECT product_id FROM product_table ORDER BY Rand()";
}
But this makes some incorrect and huge number of results and also searching takes too much of time.I don’t know why is that.I have heard about MYSQL VIEWs does it make any sense for doing this? or do i have any easy way to make this happen? thanks
You should try like this: