svgmx5 Posted June 26, 2010 Share Posted June 26, 2010 i'm trying to create a page where i can search my database for items. The field that is being searched on my database is the name of the item in there. So say i have various items with the word computer or close to it, i want to be able to type it in on a form field and pres the submit button which will query it to search the database for any row that has the word computer in its name. I've been trying the following code, but its not fully working. What i mean is that it brings results but only if its spelled exactly. $result = $_GET['key']; $category = $_GET['cat']; $get_items = "SELECT * FROM tablename WHERE category='$category' AND i_name LIKE '$result'"; $run_get = mysql_query($get_items) or die(mysql_error()); $num_rows = mysql_num_rows($run_get); I've tried using wildcards before, after and in between the variable, but i haven't had any luck. Hope someone can help me out Quote Link to comment https://forums.phpfreaks.com/topic/205907-searching-a-database-through-php/ Share on other sites More sharing options...
Alex Posted June 26, 2010 Share Posted June 26, 2010 In what ways have you tried wildcards? This is more of a MySQL question, but something like this should work: $get_items = "SELECT * FROM tablename WHERE category='$category' AND i_name LIKE '%$result%'"; Using the % wildcard, which means match 0 or more of anything. Quote Link to comment https://forums.phpfreaks.com/topic/205907-searching-a-database-through-php/#findComment-1077528 Share on other sites More sharing options...
svgmx5 Posted June 26, 2010 Author Share Posted June 26, 2010 I've tried that and the following: %$result$ %$result $result% And i keep getting nothing...so not i don't know what i'm doing wrong. Quote Link to comment https://forums.phpfreaks.com/topic/205907-searching-a-database-through-php/#findComment-1077530 Share on other sites More sharing options...
Alex Posted June 26, 2010 Share Posted June 26, 2010 Are you sure that one of the rows contains "computer", or whatever other keyword you're trying? Quote Link to comment https://forums.phpfreaks.com/topic/205907-searching-a-database-through-php/#findComment-1077531 Share on other sites More sharing options...
inversesoft123 Posted June 26, 2010 Share Posted June 26, 2010 check percentage symbol % use it both side of keyword LIKE %$result% Quote Link to comment https://forums.phpfreaks.com/topic/205907-searching-a-database-through-php/#findComment-1077532 Share on other sites More sharing options...
Alex Posted June 26, 2010 Share Posted June 26, 2010 check percentage symbol % use it both side of keyword LIKE %$result% Did you bother to read any of the posts besides the first one in this topic? If you did you would know that inversesoft123 has already tried this with no avail, so it must be something else. Quote Link to comment https://forums.phpfreaks.com/topic/205907-searching-a-database-through-php/#findComment-1077534 Share on other sites More sharing options...
svgmx5 Posted June 26, 2010 Author Share Posted June 26, 2010 well i checked and the word is misspelled...but thats what i want to do, to display a row that comes close to the word i'm looking for. i don't want to have to type in the exact word. Say i just type comp...i want that row that might say 'computer something something' to show up and so on Quote Link to comment https://forums.phpfreaks.com/topic/205907-searching-a-database-through-php/#findComment-1077536 Share on other sites More sharing options...
inversesoft123 Posted June 26, 2010 Share Posted June 26, 2010 I've tried that and the following: %$result$ %$result $result% And i keep getting nothing...so not i don't know what i'm doing wrong. In his example he is not using % on both side as you said Alex Quote Link to comment https://forums.phpfreaks.com/topic/205907-searching-a-database-through-php/#findComment-1077537 Share on other sites More sharing options...
svgmx5 Posted June 26, 2010 Author Share Posted June 26, 2010 I've tried that and the following: %$result$ %$result $result% And i keep getting nothing...so not i don't know what i'm doing wrong. In his example he is not using % on both side as you said Alex Actually i meant to type %$result%...so yea i tried that..thanks anyways Quote Link to comment https://forums.phpfreaks.com/topic/205907-searching-a-database-through-php/#findComment-1077538 Share on other sites More sharing options...
Alex Posted June 26, 2010 Share Posted June 26, 2010 well i checked and the word is misspelled...but thats what i want to do, to display a row that comes close to the word i'm looking for. i don't want to have to type in the exact word. Say i just type comp...i want that row that might say 'computer something something' to show up and so on Well with the current method if you type "comp" a row with "computer something something" should be returned. But if you want a more comprehensive search you'll have to look into more advanced techniques than just MySQL wildcards. Quote Link to comment https://forums.phpfreaks.com/topic/205907-searching-a-database-through-php/#findComment-1077540 Share on other sites More sharing options...
svgmx5 Posted June 26, 2010 Author Share Posted June 26, 2010 we'll i'll keep trying maybe i'm doing something wrong on the code....because i've looked over several tutorials and they all say pretty much the same. Anyways, thanks for the input/help. i'll leave this topic open until i can fix the issue Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/205907-searching-a-database-through-php/#findComment-1077542 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.