Kristopher Therrien
Kristopher Therrien

Reputation: 89

php mysql search data with partial match on joined tables

I am trying to search data in mysql. so far using LIKE allows me to search group names if I know the exact name I am looking for. I need to be able to search using part of the group name and have it show all rows that have some of the search string in it. below is the code I have so far.

$group = $_GET['query_term'];



if (!$link = mysql_connect('', '', '')) {

    exit;
}

if (!mysql_select_db('dbname', $link)) {

    exit;
}

$sql = "SELECT os_groups_groups.*, useraccounts.* FROM os_groups_groups JOIN useraccounts WHERE os_groups_groups.FounderID = useraccounts.PrincipalID And os_groups_groups.Name LIKE '".$group."'";

Upvotes: 1

Views: 921

Answers (1)

Adrian Cid Almaguer
Adrian Cid Almaguer

Reputation: 7791

Change your $sql to:

$sql = "SELECT os_groups_groups.*, useraccounts.* FROM os_groups_groups JOIN useraccounts WHERE os_groups_groups.FounderID = useraccounts.PrincipalID And os_groups_groups.Name LIKE '%".$group."%'";

With LIKE you can use the following two wildcard characters in the pattern:

  • % Matches any number of characters, even zero characters
  • _ Matches exactly one character

You can read more about wildcards in:

http://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html#operator_like

Upvotes: 3

Related Questions