izbryte Posted December 17, 2007 Share Posted December 17, 2007 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 https://forums.phpfreaks.com/topic/82082-solved-mysql-like-statement-to-find-something-thats-word-begins-a-d/ Share on other sites More sharing options...
lemmin Posted December 17, 2007 Share Posted December 17, 2007 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 https://forums.phpfreaks.com/topic/82082-solved-mysql-like-statement-to-find-something-thats-word-begins-a-d/#findComment-417131 Share on other sites More sharing options...
izbryte Posted December 17, 2007 Author Share Posted December 17, 2007 Thanks for your help! I got it with a little help from the link you sent my new query is: $query = "SELECT * from `info` WHERE `title` REGEXP '^[$id1-$id2]' ORDER BY `firmID` DESC"; Link to comment https://forums.phpfreaks.com/topic/82082-solved-mysql-like-statement-to-find-something-thats-word-begins-a-d/#findComment-417241 Share on other sites More sharing options...
corbin Posted December 18, 2007 Share Posted December 18, 2007 Without using regular expressions, you could do it like this (Well, I know it would work in MSSQL): SELECT * FROM info WHERE title LIKE '[abcd]%' ORDER BY firmID DESC Link to comment https://forums.phpfreaks.com/topic/82082-solved-mysql-like-statement-to-find-something-thats-word-begins-a-d/#findComment-417264 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.