0

Ive made a search function and got it working, but I have 1 problem: If I input 1 word (lets say "test") it works fine and finds the row containing "test". But if I input "test something else" it dosent find anything.

How do I make the script search for each individual word in $_POST['search'] ??

Here's my code:

$itemOutput = '';

$dbhandle = mysql_connect('host', 'name', 'pass') or die("Unable to connect to MySQL");
$selected = mysql_select_db('my_website_db', $dbhandle) or die("Could not select database");

$search_term = mysql_real_escape_string($_POST['search']);

if (!isset($search_term) || $search_term == '' || $search_term == ' ' || !ctype_alpha($search_term)) { $result_count = "0"; } else {

$fetch_items = mysql_query("SELECT * FROM products WHERE name LIKE '%{$search_term}%' OR short_info LIKE '%{$search_term}%'");

while($shop_items = mysql_fetch_array($fetch_items)) {
$item_id = $shop_items['id'];
$item_name = $shop_items['name'];
$item_price = $shop_items['price'];
$item_image = $shop_items['image']; }

$itemOutput .= '<div class="item-box"> <img src="'.$item_image.'" class="item-image" />';
$itemOutput .= '<div class="item-price"> <a style="padding-left: 15px; font-size: 14px; padding-top: 3px;">Kun </a> <a><b>'.$item_price.' kr.</b></a> </div> <br>';
$itemOutput .= '<div class="item-name"> <a>'.$item_name.'</a> </div> <br>';
$itemOutput .= '<a href="vare.php?id='.$item_id.'"><button class="item-button">Køb her &nbsp;></button></a> </div>';
}

EDIT: Thanks to all the inputs, I just got it to work :)

FrossBlock
  • 51
  • 1
  • 8
  • Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and consider using PDO, [it's not as hard as you think](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard May 08 '15 at 19:46
  • 1
    Split up the terms and append each word to it's own `like` grouping dynamically. e.g. `$query .= '(name LIKE '%{$search_term}%' OR short_info LIKE '%{$search_term}%') or '; //strip the last "or" as well` keep the escaping in or better use a driver that supports prepared statements. – chris85 May 08 '15 at 19:47
  • Each individual word or each word in that order? If in that order simply replace space with if any order as @chris85 suggests – xQbert May 08 '15 at 19:50
  • @xQbert should be each individual word in $_POST['search'] – FrossBlock May 08 '15 at 19:57
  • Consider using explode to split the string into an array and then concatenate the search criteria using or statements all wrapped together loop through the array and build or statements (slow as heck though and gets worse for each word added due to multiple OR statements.... ) maybe you can use a full text search [`match against`](https://dev.mysql.com/doc/refman/5.1/en/fulltext-boolean.html) syntax instead – xQbert May 08 '15 at 20:06
  • @xQbert Could you possible provide an example of the first method? – FrossBlock May 08 '15 at 20:12
  • I think this answer already does it. http://stackoverflow.com/questions/15538479/search-for-multiple-keywords-with-php-and-mysql-where-x-like – xQbert May 08 '15 at 20:14
  • @xQbert then how do I use this when I search both "name" and "short_info" ?? – FrossBlock May 08 '15 at 20:17
  • you have to dynamically build each string such that you have multiple or condtions wrapped in appropriate ()'s so something like `WHERE (Field1 = ar[0] OR field1 = ar[1] or field1=ar[2]) and (FIELD2 = AR2[0] OR FIELD2 = AR[1]) this way both conditions are met and the or's allow for multiple string values in any order. But this many OR's can get very slow which is I why a match against using text seraches may be a better approach. – xQbert May 09 '15 at 16:53
  • @xQbert Can you provide a script for the mentioned text search? – FrossBlock May 09 '15 at 19:34

1 Answers1

1

If you filter the search_term using ctype_alpha(), then you will get $result_count = 0. ctype_alpha() only allows [a-zA-Z]. Putting in a space will make that return false.

TJChambers
  • 1,489
  • 1
  • 18
  • 28