daled Posted July 13, 2007 Share Posted July 13, 2007 I'm trying to code something that searches a mysql database for matches to an input. The input is a get variable. here's my code: <?php $query_Search = sprintf("SELECT * FROM search"); $Search = mysql_query($query_Search, $search) or die(mysql_error()); $row_Search = mysql_fetch_assoc($Search); preg_match_all('/$_GET["searchfield"]/', $row_Search['bandname'], $match); print_r($match); ?> I believe the problem is the search patter, but i have no idea what to do with it. Thanks for anyones help. Quote Link to comment https://forums.phpfreaks.com/topic/59883-preg_match/ Share on other sites More sharing options...
MadTechie Posted July 13, 2007 Share Posted July 13, 2007 to get the preg_match_all working use preg_match_all("/{$_GET['searchfield']}/", $row_Search['bandname'], $match); but thats not the problem.. i think you want something like.. $query_Search = "SELECT * FROM search WHERE theField='{$_GET['searchfield']}'"; $Search = mysql_query($query_Search, $search) or die(mysql_error()); of course change theField to the field your searching also $search and $Search are NOT the same and $search should be the connection string Quote Link to comment https://forums.phpfreaks.com/topic/59883-preg_match/#findComment-297775 Share on other sites More sharing options...
daled Posted July 13, 2007 Author Share Posted July 13, 2007 i know what you're saying, i had your second suggestion originally, but then i thought, "hey, the searchfield would have to be EXACTLY the same as the result". that's when I decided preg_match would be better. Quote Link to comment https://forums.phpfreaks.com/topic/59883-preg_match/#findComment-297790 Share on other sites More sharing options...
teng84 Posted July 13, 2007 Share Posted July 13, 2007 dude maybe you need the loop Quote Link to comment https://forums.phpfreaks.com/topic/59883-preg_match/#findComment-297793 Share on other sites More sharing options...
MadTechie Posted July 13, 2007 Share Posted July 13, 2007 Use LIKE $query_Search = "SELECT * FROM search WHERE theField LIKE '%{$_GET['searchfield']}%'"; this will find a wider range.. note % = wildcards so %tech% will find madtechie and so will %techie and mad% you get the idea! Quote Link to comment https://forums.phpfreaks.com/topic/59883-preg_match/#findComment-297794 Share on other sites More sharing options...
MadTechie Posted July 13, 2007 Share Posted July 13, 2007 using preg will be very slow and a loop will kill the server.. use mySQL as it was ment to be used! if you MUST use MySQL REGEXP but its also slower and more limited than preg but how complex are the searches! Quote Link to comment https://forums.phpfreaks.com/topic/59883-preg_match/#findComment-297795 Share on other sites More sharing options...
trq Posted July 13, 2007 Share Posted July 13, 2007 You can also use regexs within mysql queries. See here. Quote Link to comment https://forums.phpfreaks.com/topic/59883-preg_match/#findComment-297800 Share on other sites More sharing options...
daled Posted July 14, 2007 Author Share Posted July 14, 2007 cool! regexp works! that makes me happy! one more thing now that that is fixed. is there a way to order the results by how well they match the searched expression? Quote Link to comment https://forums.phpfreaks.com/topic/59883-preg_match/#findComment-297821 Share on other sites More sharing options...
daled Posted July 14, 2007 Author Share Posted July 14, 2007 bump. Quote Link to comment https://forums.phpfreaks.com/topic/59883-preg_match/#findComment-298063 Share on other sites More sharing options...
MadTechie Posted July 14, 2007 Share Posted July 14, 2007 your need to have a index of words and count the matches Quote Link to comment https://forums.phpfreaks.com/topic/59883-preg_match/#findComment-298069 Share on other sites More sharing options...
daled Posted July 14, 2007 Author Share Posted July 14, 2007 that means? Quote Link to comment https://forums.phpfreaks.com/topic/59883-preg_match/#findComment-298075 Share on other sites More sharing options...
daled Posted July 14, 2007 Author Share Posted July 14, 2007 bump. Quote Link to comment https://forums.phpfreaks.com/topic/59883-preg_match/#findComment-298435 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.