0

hi all,

pls help me... im just a beginner in php...
there is a problem with my code. when i give one term for searching it works, but when its two shows an error message like this
Invalid SQL: SELECT * FROM tbl_candidate WHERE key_skills LIKE '%php%' OR key_skills LIKE '%net%' key_skills LIKE '%php%' OR key_skills LIKE '%net%' key_skills LIKE '%php%' OR key_skills LIKE '%net%'

here is my code

<?php

//Candidate Search

$sql = "SELECT * FROM tbl_candidate WHERE " ;
                $sql1 = "";

                 if($key_skills!="")
                {
                    $special_chars=array("+", "-", "/", ",",  "'", "\"", ";", ":", "_", "*", "&", "^", "%",".", "$",  "@", "!", "`", "(",")", "|", "\\", "<", ">", "?", ",", "{", "}", "[", "]", "=", "~","%27");


        $key_skills1 = str_replace($special_chars," ",$key_skills);
        $keywords=explode(" ",$key_skills1);            

            $count = count($keywords);
            foreach($keywords as $keywords1){

            if(strlen($keywords1)<=3 ){

            for($i=0; $i<$count; $i++){

                if($keywords[$i] !=""){
            if($i==0){
                $sql1 .="  key_skills LIKE '%$keywords[$i]%'";
            }
            else{
                $sql1 .=" OR key_skills LIKE '%$keywords[$i]%'";

                }
                $i++;
            }
            }
                //}
                }
                    //}
            else{
                $sql1 .= " MATCH(key_skills) AGAINST('+$keywords1+'IN BOOLEAN MODE)";
                }


                }
                }   
        $sql .=" $sql1";        
        echo $sql;              
?>

tnx in advnc

2
Contributors
3
Replies
4
Views
4 Years
Discussion Span
Last Post by amith_ami
0

It failed because there were no "OR" in between some likes.

From Original Query:

key_skills LIKE '%php%' OR key_skills LIKE '%net%' key_skills 
LIKE '%php%' OR key_skills LIKE '%net%' key_skills 
LIKE '%php%' OR key_skills LIKE '%net%'

Query w/ Commments:

key_skills LIKE '%php%' OR key_skills LIKE '%net%' [MISSING OR] key_skills 
LIKE '%php%' OR key_skills LIKE '%net%' [MISSING OR] key_skills 
LIKE '%php%' OR key_skills LIKE '%net%'

When Adding multiple conditional Query dynamically, you can use this pseudocode:

Prepare first condition

While Loop condition: Is there any added SQL conditions
    Is it an OR || AND
        Append Conditional Operator
    Else if non
        There's an error with the passed parameters
    Append SQL key value pair
End While

This comes in many flavor it's up to you on how you would tweak this to suit your need.

0

tnx gon1387,

but i really want to run this query only
SELECT * FROM tbl_candidate WHERE key_skills LIKE '%php%' OR key_skills LIKE '%net%';

the remaining part
key_skills LIKE '%php%' OR key_skills LIKE '%net%' key_skills LIKE '%php%' OR key_skills LIKE '%net%'
is automatically append to the actual query... i just want to remove this..
any help tnx in advnc..

0

tnx for ur help gon1387...

there is some mistakes in my code... bt some how i cleared those mistakes... and now it works.. this is my new code...

<?php
            $sql = "SELECT * FROM tbl_candidate WHERE " ;
                    $sql1 = "";

                     if($key_skills!="")
                    {

                /*$string_array = explode(" ", $key_skills);
                if($string_array):
                    $array_length = sizeof($string_array);
                else:
                    $array_length=0;
                endif;*/

                        $special_chars=array("+", "-", "/", ",",  "'", "\"", ";", ":", "_", "*", "&", "^", "%",".", "$",  "@", "!", "`", "(",")", "|", "\\", "<", ">", "?", ",", "{", "}", "[", "]", "=", "~","%27");


            $key_skills1 = str_replace($special_chars," ",$key_skills);

                //if($array_length>0):
                    $keywords=explode(" ",$key_skills1);


                    $count = count($keywords);

                for($i=0; $i<$count; $i++){
                // if($i % 2) // OR if(!($i % 2))
       // {
       //     unset($keywords[$i]);
       // }     

                if($keywords[$i] !=""){

                    if(strlen($keywords[$i])<=3 ){
                        if($i==0){
                            $sql1 .="  key_skills LIKE '%$keywords[$i]%'";
                        }
                        else{
                            $sql1 .=" OR key_skills LIKE '%$keywords[$i]%'";

                        }
                    } else{
                        if($sql1 ==""){
                        $sql1 .= "  MATCH(key_skills) AGAINST('+$keywords[$i]+'IN BOOLEAN MODE)";
                        }
                        else{
                            $sql1 .= " OR MATCH(key_skills) AGAINST('+$keywords[$i]+'IN BOOLEAN MODE)";
                            }
                    }
                }

                }

?>
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.