Hangwire Posted January 15, 2012 Share Posted January 15, 2012 Hello all. First of all, if this isn't the right place for this topic, please excuse me and may the moderators/admins move it where it should belong. Second, I'm trying to make a mysql search - Everything works, but if say, a user searches for 34 it displays all numbers that have the number 34 in them. I want the results to be exact - if a user searches for 34, to display only 34, not 334, 5034 or 3401 (like it happens now). Here's the code: <?php mysql_connect ("**************", "*******","*****") or die (mysql_error()); mysql_select_db ("********"); $term = $_POST['term']; $sql = mysql_query("select * FROM countries WHERE cocode LIKE '%$term%'"); while ($row = mysql_fetch_array($sql)){ echo '<br/> Code: '.$row['cocode']; echo '<br/> Country: '.$row['coname']; echo '<br/><br/>'; } ?> If you have any questions, you're welcome to ask them in the thread. Thank you in advance Quote Link to comment https://forums.phpfreaks.com/topic/255078-search-a-database/ Share on other sites More sharing options...
Pikachu2000 Posted January 15, 2012 Share Posted January 15, 2012 If you want to return only identical results, use =, not LIKE with wildcards. Quote Link to comment https://forums.phpfreaks.com/topic/255078-search-a-database/#findComment-1307912 Share on other sites More sharing options...
AyKay47 Posted January 15, 2012 Share Posted January 15, 2012 If you want the query to match exact strings, then like isn't what you want, use = Edit: as pickachu already stated Quote Link to comment https://forums.phpfreaks.com/topic/255078-search-a-database/#findComment-1307913 Share on other sites More sharing options...
Hangwire Posted January 15, 2012 Author Share Posted January 15, 2012 $sql = mysql_query("select * FROM countries WHERE cocode = '%$term%'"); This is the change I made, but now it doesn't give any output at all. Quote Link to comment https://forums.phpfreaks.com/topic/255078-search-a-database/#findComment-1307917 Share on other sites More sharing options...
Hangwire Posted January 15, 2012 Author Share Posted January 15, 2012 My bad, removed the wildcards at $term, it works fine now. Thanks everyone! Quote Link to comment https://forums.phpfreaks.com/topic/255078-search-a-database/#findComment-1307921 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.