I've set up a dynamic query which results in statements like this:
SELECT resume.Section_Value, candidate.Location FROM resume, candidate WHERE resume.Section_ID = '1' AND MATCH (resume.Section_Value) AGAINST ('html unix') AND candidate.Location LIKE '%CA%' OR 'California'
And I return duplicates from the same record:
html unix network php Over 10 years of HTML experience. 2 years networking administration.
html unix network php Over 10 years of HTML experience. 2 years networking administration.
I am not sure if this is an error with my sql syntax, or with my code. Can anybody assist?
Code:
//set up SELECT statement
$aSelect = array();
if($_POST['skills'] != '') {
$aSelect[] = 'resume.Section_Value';
}
if($_POST['firstname'] != '') {
$aSelect[] = 'candidate.First_Name';
}
if($_POST['lastname'] != '') {
$aSelect[] = 'candidate.Last_Name';
}
if($_POST['city'] != '' || $_POST['state'] != '') {
$aSelect[] = 'candidate.Location';
}
//$aSelect[] = 'candidate.Candidate_ID';
//set up FROM portion of sql statement
$aFrom = array();
if($_POST['skills'] != '') {
$aFrom[] = 'resume';
}
if($_POST['firstname'] || $_POST['lastname'] || $_POST['city'] || $_POST['state'] != '') {
$aFrom[] = 'candidate';
}
//set up WHERE portion of sql statement
$aWhere = array();
if($_POST['skills'] != '') {
$chars_temp = explode(" ", $_POST['skills']);
//print_r($chars_temp);
//print_r($chars);
$chars = array();
foreach($chars_temp as $key => $value) {
//only include if array is not empty
//(ie. catches empty spaces in text field entry)
if (!empty($value)) {
$chars[] = strtolower($value);
}
}
$chars = implode(" ",$chars);
$aWhere[] = "resume.Section_ID = '1' AND MATCH (resume.Section_Value) AGAINST ('" . $chars . "')";
}
if($_POST['firstname'] != '') {
$aWhere[] = "candidate.First_Name LIKE '" . $_POST['firstname'] . "%'";
}
if($_POST['lastname'] != '') {
$aWhere[] = "candidate.Last_Name LIKE '" . $_POST['lastname'] . "%'";
}
if($_POST['city'] != '' && $_POST['state'] != '') {
$aWhere[] = "candidate.Location LIKE '%" . $_POST['city'] . ", " . $state . "%' OR
'". $city . ", " . $fullstatename ."'";
}
else {
if($_POST['city'] != '') {
$aWhere[] = "candidate.Location LIKE '%" . $_POST['city'] . "%'";
}
if($_POST['state'] != '') {
$aWhere[] = "candidate.Location LIKE '%" . $state . "%' OR '". $fullstatename ."'";
}
}
//build SELECT clause
$sSelect = 'SELECT ' . implode(', ', $aSelect);
// build FROM clause
$sFrom = 'FROM ' . implode(', ', $aFrom);
// build where clause
$sWhere = 'WHERE ' . implode(' AND ', $aWhere);
// Select, Where and From clause done - add it to the SQL skeleton
//$sql2 = $sql . " " . $sWhere;
//$sql = "" . $sSelect . " " . $sFrom . " ". $sWhere;
if($_POST['skills'] != '') {
//$sql =. " AND resume.Section_ID = '1'";
$sql = "" . $sSelect . " " . $sFrom . " " . $sWhere;
} else {
$sql = "" . $sSelect . " " . $sFrom . " ". $sWhere;
}
echo $sql . "<P>";
$result = mysql_query($sql) or die(mysql_error());
//$num_rows = mysql_num_rows($result);
while($row = mysql_fetch_array($result)){
echo $row['Section_Value'] . "<P>";
}
//echo $num_rows;