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. 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
You get any error? and your code with wildcards in $result = mysql_escape_query($_GET['key']); $category = mysql_escape_query($_GET['cat']); $get_items = "SELECT * FROM tablename WHERE category='$category' AND i_name LIKE '%$result%'"; PHP:
Its hard to tell where the problem is. Can you give exact table's name and some sample data? Also, try adding `` to wrap table name and column names to see if it works. $get_items = "SELECT * FROM `tablename` WHERE `category`='$category' AND `i_name` LIKE '$result'"; Code (markup): I doubt it makes any difference but worth a short
dont forget to add wildchars unless you're searching for exact matches... $get_items = "SELECT * FROM `tablename` WHERE `category`='$category' AND `i_name` LIKE '%$result%'";
Just a novice opinion, but don't you need to include the concatenation operator in your string, like so: $get_items = "SELECT * FROM `tablename` WHERE `category`=' . $category . ' AND `i_name` LIKE '%$result%'"; Just a thought.
Well, in this instance no because in PHP when you use double quotes (") instead of single quotes (') then variables are read correctly. You CAN use it, but you need to end the quote. So in the case you've supplied it would be: $get_items = "SELECT * FROM `tablename` WHERE `category`='" . $category ." ' AND `i_name` LIKE '%$result%'"; PHP: But this could also be written as $get_items = 'SELECT * FROM `tablename` WHERE `category`=\'' . $category .'\' AND `i_name` LIKE \'%'.$result.'%\''; PHP: