[thelist] quick php/mysql search engine question

Dunstan Orchard dunstan at 1976design.com
Thu Sep 12 13:12:01 CDT 2002


Hi there,

I've got this php script that's supposed to search a MYSQL table and look for
a matching keyword in a db table field.

<html>
<head>
<title>search</title>
</head>
<body>

<?
/* connection information */
$hostname = **********";
$username =  "**********";";
$password =  "**********";";
$dbName =  "test";
$table = "images";


/* make connection to database */
MYSQL_CONNECT($hostname, $username, $password) or die( "Unable to connect to
database");

@mysql_select_db("$dbName") or die("Unable to select database");

if ($keyword == "")
	{
	$keyword = '%';
	}

$query =  ("SELECT * FROM $table WHERE keyword LIKE '$keyword%'");

$result = MYSQL_QUERY($query);

if (!$result) print ("failed\n");


/* Determine the number of records returned */
while ($row = MYSQL_FETCH_ROW($result))
$number = mysql_numrows($result);

if (!$number)
	{
	print("There are no results that match your query");
	}
else
	{
	/* Print the relevant information */
	$i = 0;
	print  "There are $number entries found";
	print  "<table cellspacing=\"0\" cellpadding=\"5\" border=\"1\">";

	while ($i < $number)
		{
		$name = mysql_result($result, $i,"name");
		$keyword = mysql_result($result, $i,"keyword");
		if ($i%2 == 0)
			{
			print "<tr
bgcolor=\"#eeeeee\"><td>$name</td><td>$keyword</td></tr>";
			}
		else
			{
			print "<tr
bgcolor=\"#ffffff\"><td>$name</td><td>$keyword</td></tr>";
			}
		$i++;
		}
	print "</table>";
	}

/* Close the database connection */
MYSQL_CLOSE();
?>

</body>
</html>



The db table field is called 'keyword'.
The variable that will be passed from a form (eventually) is also called
keyword.

Each db record has a 'keyword' entry list that looks something like this:

"bird, flight, gull, seagull, beach"

a bunch of words seperated by commas.


If I search for 'bird' then the above example would be found:
http://www.1976design.com/testarea/example.php?keyword=bird

but if I search for flight, it won't be found:
http://www.1976design.com/testarea/example.php?keyword=flight


How do I alter this script so it looks through all the entries in
the 'keyword' field?


Do I have to put them into a array or something?


Thanks for any help - could you cc me in your reply as I'm on digest,

Many thanks - dunstan

---------------------------
Dorset, England
http://www.1976design.com/
http://www.orchard.it/
http://www.maccaws.org/



More information about the thelist mailing list