Solved

generate a dynamic mysql query

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

Accepted Solution

by:
Pawan Kumar Khowal earned 500 total points
Comment Utility
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
Comment Utility
yes its work. thanx
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

772 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

10 Experts available now in Live!

Get 1:1 Help Now