Jump to content

searching a database through PHP


svgmx5

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.