0

I'm using this search function inside the search controller to send search results to the web page. For some reason it doesn't work. I'm using PDO and i can't see any errors in the php log either. I'we spent a lot of time trying to figure out what's wrong and still seems as if its a lost cause

public function search($search_string){

if (strlen($search_string) >= 1 && $search_string !== ' ') {
    // Build Query
    $sql = 'SELECT * FROM products WHERE name LIKE "%:search%" OR description LIKE "%:search%"';
    $query = $this->db->prepare($sql);
    $query->execute(array(':search' => $search_string));
    $this->search=$query->fetchAll();
    // Do Search


    // Check If We Have Results
    if (isset($query)) {

foreach($this->search as $key => $value) {
            // Format Output Strings And Hightlight Matches
            $display_function = preg_replace("/".$search_string."/i", "<b class='highlight'>".$search_string."</b>", $value->name);
            $display_name = preg_replace("/".$search_string."/i", "<b class='highlight'>".$search_string."</b>", $value->name);
            $display_url = 'http://hello.com/what'.urlencode($value->name).'&lang=en';

            // Insert Name
            $output = str_replace('nameString', $display_name, $html);

            // Insert Function
            $output = str_replace('functionString', $display_function, $output);

            // Insert URL
            $output = str_replace('urlString', $display_url, $output);

            // Output
            echo($output);
        }
    }else{

        // Format No Results Output
        $output = str_replace('urlString', 'javascript:void(0);', $html);
        $output = str_replace('nameString', '<b>No Results Found.</b>', $output);
        $output = str_replace('functionString', 'Sorry :(', $output);

        // Output
        echo($output);
    }
}





}
BenMorel
  • 34,448
  • 50
  • 182
  • 322

1 Answers1

0

Change this part:

$sql = 'SELECT * FROM products WHERE name LIKE "%:search%" OR description LIKE "%:search%"';
$query = $this->db->prepare($sql);
$query->execute(array(':search' => $search_string));

to:

$sql = 'SELECT * FROM products WHERE name LIKE :search OR description LIKE :search';
$query = $this->db->prepare($sql);
$query->execute(array(':search' => "%".$search_string."%"));

Your way to use placeholder is not valid.

dlyaza
  • 238
  • 2
  • 9