homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Databases
Forum Library, Charter, Moderator: open

Databases Forum

match against
but only if the against string starts with the string to match

 3:26 pm on Jun 18, 2009 (gmt 0)

I need to use match against in the following circumstances:

MATCH ('widget') AGAINST ('widget text is here')

this should succeed as the string in the against function starts with widget.

MATCH ('widget') AGAINST ('text for widget is here')

this should fail as the string in the against function does not start with widget. right now they both return results.

any ideas anyone?



 3:49 pm on Jun 18, 2009 (gmt 0)

Sounds like you need something like a "startswith" function.

Search your db user guide for a "startswith" or "starts with" function.


 4:29 pm on Jun 18, 2009 (gmt 0)

Well, I'll use mySQL as an example (state your DB type as inferred . . . )

Front end form:

<input type="radio" name="match_type" id="match_type_any" value="0" checked> <label for="match_type_any">Anywhere</label>
<input type="radio" name="match_type" id="match_type_exact" value="1"> <label for="match_type_exact">Exact</label>
<input type="radio" name="match_type" id="match_type_start" value="2"> <label for="match_type_start">Starts With</label>
<input type="radio" name="match_type" id="match_type_end" value="3"> <label for="match_type_end">Ends With</label>

Then in your search script, set the "comparator" based on what's submitted (perl example shown, convert as required . . . of course filter data before you get to this point.)


if ($search_type == 1) {
$comparator = '=';
$value = $data{'search_term'};
elsif ($search_type == 2) {
$comparator = ' like ';
$value = qq(%$data{'search_term'});
if ($search_type == 3) {
$comparator = ' like ';
$value = qq($data{'search_term'}%);
## Zero or null
else {
$comparator = ' like ';
$value = qq(%$data{'search_term'}%);

$select = qq(select * from table where field $comparator '$value';);

Which gives you one of four:
1, exact
select * from table where field = 'search term';
2, starts with
select * from table where field like '%search term';
3, ends with
select * from table where field like 'search term%';
0 or null, anywhere
select * from table where field like '%search term%';

I used a specific field, you could use match against or any variation of allowing user-selected fields.


 5:09 pm on Jun 18, 2009 (gmt 0)

many thanks.

mysql! - hours spent desperately trying to sort it then a rushed posting to WebmasterWorld lol.


 9:47 pm on Jun 18, 2009 (gmt 0)

Does this mean you solved it? For later visitors the solution would be helpful . . . and which database type you're working with . . .

Noted an error in my sample, this

if ($search_type == 3) {


elsif ($search_type == 3) {


 10:23 pm on Jun 18, 2009 (gmt 0)

yeah I got there in the end.

its a MyISAM db.

the "startswith" comment made me completely re-think and I ended up with:

SELECT widget FROM widgets WHERE LOCATE(widget, 'text to match') = 1

for some reason I was thinking it was going to be a really complex query.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Databases
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved