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";

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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