Solved

generate a dynamic mysql query

Posted on 2016-10-10
2
46 Views
Last Modified: 2016-10-10
php code where I am trying to generate a dynamic mysql query for https://www.experts-exchange.com/questions/28975524/Get-error-when-search-query-run-in-loop.html?anchor=a41837368#a41837368
The query is giving me error.
php code is

if($skillsMain) {
		// Search for employees from Table: employee_info
		 $sql_search = "SELECT * FROM employee_info WHERE 1 = 1 ";
		if($city!='') {
			$sql_search .= " AND city='$city' ";
		}
		
		if($skillsMain!='' && $skillsMain!='.' & $skillsMain!=',') {
			$sql_search .= " AND ( ";
			$skillsArr = explode(',', $skillsMain); 
			$arrCount = count($skillsArr);
			$counter = 1;
			foreach($skillsArr as $skills){
				$skills = trim($skills);
				$sql_search .=  "( key_skills LIKE '%$skills%' OR  job_title LIKE '%$skills%' OR  current_designation LIKE '%$skills%' OR  company_name LIKE '%$skills%' OR  objective LIKE '%$skills%' OR  resum_headline LIKE '%$skills%' OR  job_responsibility LIKE '%$skills%' OR  institute LIKE '%$skills%' OR  institute LIKE '%$skills%')";
				if($counter < $arrCount) {
					$sql_search .=  " OR ";
				}
				$counter ++;
			}
			$sql_search .= " ) ";
			
		}

		// Filter by Experience
		if($exp_min && $exp_max) {
			$sql_search .= " AND total_experience between $exp_min and $exp_max";
		} else if($exp_min) {
			$sql_search .= " AND total_experience > $exp_min";
		} else if($exp_max) {
			$sql_search .= " AND total_experience < $exp_max";
		} else if($experience!='')
		{
			$sql_search .= " AND total_experience=$experience";
		}
		// Filter By Salary
		if($sal_min && $sal_max) {
			$sql_search .= " AND salary between $sal_min and $sal_max";
		} else if($sal_min) {
			$sql_search .= " AND salary > $sal_min";
		} else if($sal_max) {
			$sql_search .= " AND salary < $sal_max";
		} else if($salary!='')
		{
			$sql_search .= " AND salary='$salary'";
		}
		
		if($func!='') {
			$sql_search .= " and functional_area='$func'";
		}
		if($inds!='') {
			$sql_search .= " and industry='$inds'";
		}
		if($qual!='') {
			$sql_search .= " and qualification='$qual'";
		}
	// for keywords
	 if($keywords!='' && $refineskills!='' ) {
		$sql_search .= " and job_description LIKE '%".$keywords."%' or  skills LIKE '%".$keywords."%' ";
	}

		$sql_search .= " GROUP BY  uid DESC";
		$sql_search .=  " UNION ";
		$arrCount1 = count($skillsArr);
		$counter1 = 1;
		foreach($skillsArr as $skills1){
				$skills1 = trim($skills1);
				$sql_search .= "(SELECT `id` , `uid` ,  `pre_company_name` ,  `pre_industry` ,  `pre_functional_area` ,  `pre_job_date` ,  `pre_job_date_month` ,  `pre_responsibility`   FROM prev_companies WHERE pre_company_name LIKE '%".$skills1."%' OR pre_industry LIKE '%". $skills1 ."%' OR pre_functional_area LIKE '%". $skills1 ."%' OR pre_responsibility LIKE '%". $skills1 ."%')";
				if($counter1 < $arrCount1) {
					$sql_search .=  " OR ";
				}
				$counter1 ++;
			} 
			
		
		echo $sql_search;

Open in new window

0
Comment
Question by:Amita Singh
2 Comments
 
LVL 28

Accepted Solution

by:
Pawan Kumar earned 500 total points
ID: 41837424
I am not an expert in php but i think this will work. Pls try.

---------

if($skillsMain) 
{
		// Search for employees from Table: employee_info
		
		 $sql_search = " SELECT `id` , `uid` ,  `company_name` , `industry` ,  `functional_area` , `current_job_date` , `current_job_date_month` , `job_responsibility` FROM employee_info WHERE 1 = 1 ";
		 
		if($city!='') 
		{
			$sql_search .= " AND city='$city' ";
		}
		
		if($skillsMain!='' && $skillsMain!='.' & $skillsMain!=',') 
		{
			
			$sql_search .= " AND ( ";
			
			$skillsArr = explode(',', $skillsMain); 
			
			$arrCount = count($skillsArr);
			
			$counter = 1;
			
			foreach($skillsArr as $skills)
			{
				$skills = trim($skills);
				$sql_search .=  
				
				" (    key_skills LIKE '%$skills%' OR  job_title LIKE '%$skills%' OR  current_designation LIKE '%$skills%' 
				   OR company_name LIKE '%$skills%' OR  objective LIKE '%$skills%' OR  resum_headline LIKE '%$skills%' 
				   OR job_responsibility LIKE '%$skills%' OR  institute LIKE '%$skills%' OR  institute LIKE '%$skills%' ) ";
				
				if($counter < $arrCount) 
				{
					$sql_search .=  " OR ";
				}
				
				$counter ++;
			}
			
			$sql_search .= " ) ";
			
		}
		
		// Filter by Experience
		
		if($exp_min && $exp_max) 
		{
			$sql_search .= " AND total_experience between $exp_min and $exp_max";
		} 
		else if($exp_min) 
		{
			$sql_search .= " AND total_experience > $exp_min";
		} 
		else if($exp_max) 
		{
			$sql_search .= " AND total_experience < $exp_max";
		} 
		else if($experience!='')
		{
			$sql_search .= " AND total_experience = $experience";
		}
				
		// Filter By Salary
		
		if($sal_min && $sal_max) 
		{
			$sql_search .= " AND salary between $sal_min and $sal_max";
		} 
		else if($sal_min) 
		{
			$sql_search .= " AND salary > $sal_min";
		} 
		else if($sal_max) 
		{
			$sql_search .= " AND salary < $sal_max";
		} 
		else if($salary!='')
		{
			$sql_search .= " AND salary='$salary'";
		}
		
		if($func!='') {
			$sql_search .= " AND functional_area='$func'";
		}
		if($inds!='') {
			$sql_search .= " AND industry='$inds'";
		}
		if($qual!='') {
			$sql_search .= " AND qualification='$qual'";
		}
	
	
	  // for keywords
	 
	 if($keywords!='' && $refineskills!='' ) 
	 {
		$sql_search .= " AND ( job_description LIKE '%".$keywords."%' OR skills LIKE '%".$keywords."%' ) ";
	 } 

	$sql_search .=  " UNION ";
	
	
	$sql_search .= " SELECT `id` , `uid` ,  `pre_company_name` ,  `pre_industry` ,  `pre_functional_area` ,  `pre_job_date` ,  `pre_job_date_month` ,  `pre_responsibility` FROM prev_companies 
			WHERE 1 = 1 "
	
	
	$skillsArr = explode(',', $skillsMain);
	
	if($skillsArr!='' && $skillsArr!='.' & $skillsArr!=',') 
	{

			$sql_search .= " AND ( ";

				$arrCount1 = count($skillsArr);
				$counter1 = 1;
				foreach($skillsArr as $skills1)
				{
						$skills1 = trim($skills1);
						
						$sql_search .= " ( pre_company_name LIKE '%".$skills1."%' OR pre_industry LIKE '%". $skills1 ."%' OR pre_functional_area LIKE '%". $skills1 ."%' OR pre_responsibility LIKE '%". $skills1 ."%' ) ";						
						
						if($counter1 < $arrCount1) 
						{
							$sql_search .=  " OR ";
						}
						
						$counter1 ++;
				} 

			$sql_search .= " ) ";

	}	
		
echo $sql_search;

---------

Open in new window


Hope it helps..
0
 
LVL 2

Author Closing Comment

by:Amita Singh
ID: 41837433
yes its work. thanx
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question