Solved

generate a dynamic mysql query

Posted on 2016-10-10
2
40 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 23

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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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.

943 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now