I am eager to put a search tool on my site, however there is a slight problem.... I'm a PHP NOOB. :-O Can anybody point me to a easy, but sufficient tutorial on how to implement the search feature on your site? Or even give me steps on here? Anything will help and will be much appreciated! Thanks.

Recommended Answers

All 5 Replies

$term = $_POST['searchterm'];
$dat = mysql_query("SELECT * FROM tbl WHERE colum LIKE '%$term'") or die(mysql_error());
if($data = mysql_fetch_array($dat))
echo mysql_num_rows($dat)." result(s) found";
do{
// display results with echo
}
while($data = mysql_fetch_array($dat));
}
else{
echo "Sorry no results found :(";
}

Thanks for the code Architact. I am a bigger NOOB than that though. Do I have to do something with MySQL? Also, that code do I just put in a search.php file for example? Any help is appreciated.

set the form action to search.php and set the id of search field to searchterm.

<?php
//connect your server
mysql_connect("localhost","username","password");
//connect your db
mysql_select_db("database");

$term = $_POST['searchterm'];
//in the below line replace tbl with the table name and the column with the column name(you want to search) in that table
$dat = mysql_query("SELECT * FROM tbl WHERE colum LIKE '%$term'") or die(mysql_error());
if($data = mysql_fetch_array($dat))
echo mysql_num_rows($dat)." result(s) found";
do{
echo $data['id']; // replace id with the column you want to display as result
}
while($data = mysql_fetch_array($dat));
}
else{
echo "Sorry no results found :(";
}
?>

use this code in your search.php
also read this

Ok, so far I think I understand you. However, I don't have a database setup yet. What do I need to do to setup the database? Is there a nice program that helps organize it for you. I have PHPMyAdmin through hosting.

If you don't want database included then try using google search for your site, I don't know exactly how to put it on the site, but google is your friend. And if you want to use database apache2triad is your friend. download it, install it and then i will lead you further.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.