Jump to content

Search Functions


JSHINER

Recommended Posts

Using the following:

 

SELECT * FROM table WHERE name LIKE sandra bolton OR city LIKE sandra bolton

 

Does not return any results even though there is a Sandra in the system, and she is from Bolton.

 

However, if I do name LIKE sandra OR city LIKE bolton - it works. Is there anyway to fix this?

 

Problem is I want one search box, not a name and city box.

Link to comment
Share on other sites

Your first query is simply invalid if you want to search two fields of the database. You'll need to explode your form data into the two required parts. eg;

 

<?php

  if (isset($_POST['search'])) {
    $arr = explode(' ',$_POST['search']);
    $name = $arr[0];
    $loc = $arr[1];
  }

?>

 

This method needs some work, but its a start.

Link to comment
Share on other sites

Or like this:

 

<?php
$remove_vals = array('/,/','/ /');  #Strings you would like to replace with a % sign. to use in the like statement.
$variable = 'sandra bolton, New York';
$nv = preg_replace($remove_vals,'%',$variable);
echo "SELECT * FROM `table` WHERE `name` LIKE '$nv' OR city LIKE '$nv'";
?>

Link to comment
Share on other sites

Or like this:

 

<?php
$remove_vals = array('/,/','/ /');  #Strings you would like to replace with a % sign. to use in the like statement.
$variable = 'sandra bolton, New York';
$nv = preg_replace($remove_vals,'%',$variable);
echo "SELECT * FROM `table` WHERE `name` LIKE '$nv' OR city LIKE '$nv'";
?>

 

This returns

SELECT * FROM `table` WHERE `name` LIKE 'sandra%bolton%%New%York' OR city LIKE 'sandra%bolton%%New%York'

 

And although there is a sandra bolton in new york, it does not return any results.

Link to comment
Share on other sites

maybe these extra % signs will work in the query string.

<?php
$remove_vals = array('/,/','/ /');  #Strings you would like to replace with a % sign. to use in the like statement.
$variable = 'sandra bolton, New York';
$nv = preg_replace($remove_vals,'%',$variable);
echo "SELECT * FROM `table` WHERE `name` LIKE '%$nv%' OR city LIKE '%$nv%'";
?>

 

btw: the above wasn't supposed to return results (if that was what you were expecting), just output a select string.

Link to comment
Share on other sites

Creating a string like "%term1%term2%" won't work ideally because it'll only match both terms, in order.  Like I said earlier, you'll need to separate all terms and search for them with OR conditions.  If you're using commas as separators, then preg_split with '/\s*,\s*/' and build a query string.  You should be able to figure out how to assemble a string.

 

$columns = array('city','name');
$terms = array();
foreach (preg_split('/\s*,\s*/',$search_string,-1,PREG_SPLIT_NO_EMPTY) as $term) {
  foreach ($columns as $col) $terms[] = "$col LIKE '%$term%'";
}
$query = 'SELECT * FROM TABLE WHERE ' . implode(' OR ',$terms);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.