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.
columnname LIKE '%something%'i.e. the string need to be inside single quoteslastName LIKE ?in your prepare and then bind$argto that parameter.$searchcoming from? Can you try entering "John Smith" and thenechothe$wherevariable to see what's going on?