Jump to content

[SOLVED] MySQL LIKE statement to find something that's word begins A-D?


izbryte

Recommended Posts

I have a page with an alphabetical search. So if they click on "A-C" the variable for $id1 is A and $id2 is C.

Then from the database they should get everything that is titled with an A, B or C.

 

Do I use the LIKE statement to do this? Here's what I have now and it's not working:

$query = "SELECT * from `info` WHERE `title` LIKE ('$id1%'-'$id2%') ORDER BY `ID` DESC";

You can use regular expressions in MySQL: http://dev.mysql.com/doc/refman/5.1/en/regexp.html

 

I don't think I have ever actually done it, but I think you want something like this:

$query = "SELECT * from `info` WHERE `title` REGEXP '[^[$id1-$id2]]' ORDER BY `ID` DESC";

 

That should match anything that starts with $id1 through $id2, but look into the regex more if it doesn't work as expected.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.