1

I'm having trouble getting a query to display results if there are spaces in the search input. The database has a column that has two strings in it. The issue is that I don't know how to use LIKE to filter information if it has spaces in the string. I'm not sure if MATCH AGAINST is needed for this since I'm not interested in two separate columns of data in the table, I'm interested in one column that has two words separated by a space, the column is fullNameNormal.

EDIT: I just realized that I forgot that the search feature is actually the input that is for $search. Originally documented it as $letter, but that is for a letter search only.

$search = '';
        if(isset($_REQUEST['search'])){
            $search = substr($this->encode($_REQUEST['search']), 0, 50);
        }
$letter = '';
if (isset($_REQUEST['letter'])) {
    $letter = substr($_REQUEST['letter'], 0, 1);
}
// Get total amount of records for current search...for paging
$total = 0;
$sql = "SELECT COUNT(*) FROM " . $wpdb->prefix . "example_directory";
$clean_where = " WHERE dirListing = 'public' AND active = 1 AND (((employeeType = 'faculty') AND (shortPositionCode NOT IN ('47', '49', '58', '59')) AND (status IN ('fullTime', 'partTime', 'proRata'))) OR ((employeeType = 'staff') AND (shortPositionCode NOT IN ('47', '49', '58', '59')) AND (status IN ('fullTime', 'partTime'))))";
$order = " ORDER BY lastName, firstName, department";
$limit_query = $wpdb->prepare(" LIMIT %d, %d", $start, $limit);
$where = "";
$args = array();
if ($search != '') {
    $where = " AND (lastName LIKE %s OR firstName LIKE %s OR department LIKE %s OR fullName LIKE %s OR nickName LIKE %s OR jobTitle LIKE %s OR phoneExt LIKE %s OR phone LIKE %s OR email LIKE %s OR locationBuilding LIKE %s OR locationBuildingAbbrev LIKE %s OR locationRoom LIKE %s or fullNameNormal LIKE %s)";
    $arg = '%' . $search . '%';
    $args = array($arg, $arg, $arg, $arg, $arg, $arg, $arg, $arg, $arg, $arg, $arg, $arg, $arg);
} elseif ($letter != '') {
    $where = " AND lastName LIKE %s";
    $arg = $letter . '%';
    $args = array($arg);
} else {
    $where = "";
}
    $where = $wpdb->prepare($where, $args);
    $total = $wpdb->get_var($sql . $clean_where . $where . $order);

As previously stated, I'm only interested in fullNameNormal which would be as an example "John Smith" in the cell, but it seems that there's no way to use LIKE and limit it to only what I put in the input that becomes $search. I also tried adding MATCH and AGAINST like this -

$where = " AND (lastName LIKE %s OR firstName LIKE %s OR department LIKE %s OR fullName LIKE %s OR nickName LIKE %s OR jobTitle LIKE %s OR phoneExt LIKE %s OR phone LIKE %s OR email LIKE %s OR locationBuilding LIKE %s OR locationBuildingAbbrev LIKE %s OR locationRoom LIKE %s OR MATCH(fullNameNormal) AGAINST (".$search."))";

EDIT: Also tried this -

$where = " AND (lastName LIKE %s OR firstName LIKE %s OR department LIKE %s OR fullName LIKE %s OR nickName LIKE %s OR jobTitle LIKE %s OR phoneExt LIKE %s OR phone LIKE %s OR email LIKE %s OR locationBuilding LIKE %s OR locationBuildingAbbrev LIKE %s OR locationRoom LIKE %s OR (fullNameNormal LIKE 'something%' AND fullNameNormal LIKE '% something%')";

Ended up query everything despite what I put in the search input.

I'm unsure of what I need to do here.

4
  • You need to use columnname LIKE '%something%' i.e. the string need to be inside single quotes Commented Sep 9, 2015 at 18:06
  • 1
    FYI: You can do lastName LIKE ? in your prepare and then bind $arg to that parameter. Commented Sep 9, 2015 at 18:08
  • Where is $search coming from? Can you try entering "John Smith" and then echo the $where variable to see what's going on? Commented Sep 9, 2015 at 18:42
  • You should really be taking a natural language search approach to this problem. dev.mysql.com/doc/refman/5.7/en/fulltext-natural-language.html Commented Sep 9, 2015 at 19:45

1 Answer 1

1

You should really be taking a natural language search approach to this problem. A query using this approach would look like:

SELECT COUNT(*) FROM *example_directory
WHERE MATCH (
  lastName,
  firstName,
  department,
  fullName,
  nickName,
  jobTitle,
  phoneExt,
  phone,
  email,
  locationBuilding,
  locationBuildingAbbrev,
  locationRoom,
  fullNameNormal
) AGAINST (? IN NATURAL LANGUAGE MODE)
AND  ... /* other filter conditions */

Where ? is your search phrase.

You will need to create a FULLTEXT index on the columns in your search.

See MySQL Documentation.

Side note: I have no idea why you would be applying ordering or limits in your query within the context of COUNT(*) not being able to return more than one row without any additional fields in select.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I looked over the documentation in your comment and agree with this solution, this was built by someone else and I was just requested to change the functionality to include FULLTEXT indexing but have never done that before.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.