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
Solved

generate a dynamic mysql query

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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.
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

839 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