Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Need Help Building a Search Site Form (PHP,MySQL)

Need a form that searches (queries) a MySQL database



4:48 am on Jan 7, 2005 (gmt 0)

10+ Year Member


I have created a MySQL / PHP publishing database. I want to provide a search.php form that can search the database for certain fields:

- Title
- Date
- Author
- Section

Does anyone know a handy PHP script that will search and print search results based on certain form fields?

Also, how to format them so it shows 20 words from the field 'content'.



4:54 am on Jan 7, 2005 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

what about a few handy dandy threads?

Basics of extracting data from MySQL using PHP [webmasterworld.com]
syntax for selecting first 100 characters from a mysql field [webmasterworld.com]


5:45 am on Jan 7, 2005 (gmt 0)

10+ Year Member

Thank you Jatar_K...

The second link I found super useful. I am going to read through the first link (forum topic) later tonight and I have found several others from this forum:


I am working on some test scripts now that I will share for workshopping very soon.

IN the meantime, I hope for continued suggestions or recommendations. Thanks again Jatar_K!

Back in a bit...



12:23 am on Jan 8, 2005 (gmt 0)

10+ Year Member

Here is what I got so far and it works!


<form method="post" action="search_results.php">
<table width=88% align=center class="border_table">
<tr bgcolor="#55556F">
<td width="22%" bgcolor="#55556F"><p>Search for:</p></td>
<td width="78%"><input type=text name='search' size=60 maxlength=255></td></tr>
<tr bgcolor="#55556F"><td></td>
<td bgcolor="#55556F"><input type=submit></td>




if ($search) // perform search only if a string was entered.
mysql_connect() or die ("Problem connecting to Database");

$query = mysql_query("SELECT wname, url, title, keywords, description, city, country, category FROM directory WHERE city LIKE '%$search%' wname LIKE '%$search%' title LIKE '%$search%' keywords LIKE '%$search%' description LIKE '%$search%' country LIKE '%$search%' category LIKE '%$search%' url LIKE '%$search%'", $db);

if ($query)
echo "<table width=100% border=0 cellpadding=0 cellspacing=5 class=border_playlist_table><tr>";

while ($myrow = mysql_fetch_array($query)) { // Begin while
$wname = $myrow["wname"];
$url = $myrow["url"];
$title = $myrow["title"];
$city = $myrow["city"];
$country = $myrow["country"];
$category = $myrow["category"];
$keywords = $myrow["keywords"];
$description = $myrow["description"];
echo "<tr class=sub_1><td colspan=4>$wname : $category</td></tr>
<tr bgcolor=#55556F><td><p><a href=$url target=_blank>$url</a></p></td>
<tr bgcolor=#55556F><td colspan=4>$keywords</td></tr>
<tr bgcolor=#55556F><td colspan=4>$description</td></tr>
<tr><td colspan=4>&nbsp;</td></tr>";
} // end while
echo "</tr></table>";
} else { echo "problems...."; }
} else {
echo "Search string is empty. <br> Go back and type a string to search";

Thanks for all your help!



12:36 am on Jan 8, 2005 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

nice work :)

Featured Threads

Hot Threads This Week

Hot Threads This Month