Need search within site code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

mikewooten
Forum Contributor
Posts: 169
Joined: Wed Feb 11, 2004 12:13 pm
Location: Duluth, Georgia
Contact:

Need search within site code

Post by mikewooten »

i'm working on an e-commerce website for a school project and it requires a search within a site.
example: you go to somesite.com and they have a search text box and it searches for items within the site. thats what i'm looking for.

does anyone know how to do that, or does anyone know where to go to get code for that or anyone have any sample code that they could give me for a search query for a site? or could someone help me create a search query?
any help would be appreciated.
thanks
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

are the items you searching for within a MyQL database? are they in a text file? are they just in the body of html?

you need to be more specific due to theres certain method you can go about
mikewooten
Forum Contributor
Posts: 169
Joined: Wed Feb 11, 2004 12:13 pm
Location: Duluth, Georgia
Contact:

Post by mikewooten »

the items that i have within my site are stored in a mysql database.
here's the site i'm working on


http://www.wootenmedia.com/wootenmusic7/guitars.php
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

I think you would have to do a query to search for specific words in the database! I see you have:

search.html?search=paint&submit=SUBMIT

All you would need to do is $_GET[''] the specific keyword, which is paint in the example above. Then do a mysql query to search for records which are LIKE the keywords.
mikewooten
Forum Contributor
Posts: 169
Joined: Wed Feb 11, 2004 12:13 pm
Location: Duluth, Georgia
Contact:

Post by mikewooten »

what would i have to use in the mysql query?
would i use a select statement?
would it be something like
SELECT * FROM db WHERE paint LIKE?
how would that mysql query look?
thanks?
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Try this:

<?php
$item = $_GET['search'];
$link = mysql_connect("$host","$user","$pass") or die("Connection Error");
mysql_select_db($dbname) or die("Mysql Error!");
$query = "SELECT * FROM db WHERE itemname LIKE '%'$item'%'";
$result = mysql_query($query) or die("Query Error!");

etc, etc...


Joe 8)
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

FYI

the % symbol is a wildcard.

so ti% would return
tim
timmy
timm
timmy
tim?anything
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

you can use google to search within a domain
mikewooten
Forum Contributor
Posts: 169
Joined: Wed Feb 11, 2004 12:13 pm
Location: Duluth, Georgia
Contact:

?

Post by mikewooten »

i used the code that you gave me above:

Code: Select all

<form action="<? $_SERVER['PHP_SELF']; ?>">
	&nbsp;&nbsp;&nbsp;<font size="5" color="white"><strong>Search</strong></font>
      <input type="text" name="search" size="13" maxlength="99">
	<input type="submit" name="submit" value="SUBMIT" width="10" height="1">
</form>





<?php 
$host = "localhost";
$user = "username";
$pass = "password";
$tblname = "items";
$dbname = "db";
$item = $_GET['search']; 
$link = mysql_connect("$host","$user","$pass") or die("Connection Error"); 
mysql_select_db($dbname) or die("Mysql Error!"); 
$query = "SELECT * FROM $tblname WHERE itemName LIKE '%'$itemName'%'"; 
$result = mysql_query($query) or die("Query Error!"); 
echo "$result";
?>

and it returns a result as Resource id #3.
why is it giving me this result?
also what else would i have to use in my code for the search, to search the mysql database and return results?
thanks
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Don't forget RLIKE and MATCH. No wait a minute, forget all about MATCH...
mikewooten
Forum Contributor
Posts: 169
Joined: Wed Feb 11, 2004 12:13 pm
Location: Duluth, Georgia
Contact:

Post by mikewooten »

what's RLIKE? how would i use it in the query that i already have? or would i use RLIKE instead of like?
if i were to use RLIKE in the query that i already have, where would i put it?
thanks
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Try:

<?php
$host = "localhost";
$user = "username";
$pass = "password";
$tblname = "items";
$dbname = "db";
$item = $_GET['search'];
$link = mysql_connect("$host","$user","$pass") or die("Connection Error");
mysql_select_db($dbname) or die("Mysql Error!");
$query = "SELECT * FROM $tblname WHERE itemName LIKE '%'$item'%'";
$result = mysql_query($query) or die("Query Error!");
echo "$result";
?>

Joe 8)
mikewooten
Forum Contributor
Posts: 169
Joined: Wed Feb 11, 2004 12:13 pm
Location: Duluth, Georgia
Contact:

Post by mikewooten »

ok. now that i changed it to '%'$item'%' like in your example that you posted, i tried that, and the file returned as Query Error!, so it seems like the sql is failing? how could i get it to work, what could i try?
thanks
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

TRY:

<?php
$host = "localhost";
$user = "username";
$pass = "password";
$tblname = "items";
$dbname = "db";
$item = $_GET['search'];
$link = mysql_connect("$host","$user","$pass") or die("Connection Error");
mysql_select_db($dbname) or die("Mysql Error!");
$query = "SELECT * FROM $tblname WHERE itemname = '$item'";
$result = mysql_query($query) or die("Query Error!");
echo "$result";
?>

That is getting rid of the like query altogether. It should work but im not sure... The above query will only look for exact keywords!
mikewooten
Forum Contributor
Posts: 169
Joined: Wed Feb 11, 2004 12:13 pm
Location: Duluth, Georgia
Contact:

Post by mikewooten »

i changed the select statement and this is the output, Resource id #3,
what else should i try for this to work?
thanks
Post Reply