?
Solved

generate a dynamic mysql query

Posted on 2016-10-10
2
Medium Priority
?
50 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 29

Accepted Solution

by:
Pawan Kumar earned 2000 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
The viewer will learn how to dynamically set the form action using jQuery.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

801 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