Page 1 of 2

Need search within site code

Posted: Tue May 25, 2004 1:44 pm
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

Posted: Tue May 25, 2004 2:27 pm
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

Posted: Tue May 25, 2004 3:45 pm
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

Posted: Tue May 25, 2004 4:19 pm
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.

Posted: Tue May 25, 2004 7:02 pm
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?

Posted: Tue May 25, 2004 7:47 pm
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)

Posted: Tue May 25, 2004 9:55 pm
by tim
FYI

the % symbol is a wildcard.

so ti% would return
tim
timmy
timm
timmy
tim?anything

Posted: Wed May 26, 2004 7:07 am
by magicrobotmonkey
you can use google to search within a domain

?

Posted: Wed May 26, 2004 5:06 pm
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

Posted: Wed May 26, 2004 6:37 pm
by McGruff
Don't forget RLIKE and MATCH. No wait a minute, forget all about MATCH...

Posted: Wed May 26, 2004 6:46 pm
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

Posted: Wed May 26, 2004 8:03 pm
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)

Posted: Wed May 26, 2004 8:10 pm
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

Posted: Wed May 26, 2004 8:22 pm
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!

Posted: Wed May 26, 2004 8:51 pm
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