NetAdminTools.com
 
SignalQ Sites:
NetAdminTools - Coprolite - NoNIC - SpotBridge - NAW
RoboCoop - AreWeDown - SolarPower - SysAdminTools
Xfig - Gold Loaf - GeekPapa - FixGMC - MCJ - FixRambler
Categories:
GNU/Linux | Homebrew designs | Perl | Administration | Backup/Recovery | Bugs/Fixes | Certification | Database | Email | File/Print | Hardware | Information Grab Bag | Interoperability | GNU/Linux ABCs | Monitoring | Name Resolution | Network Services | Networking | Remote Control | Security | Desktop | Web | BSD | Solaris | GIAGD | REALbasic

Last 30 Days | Last 60 Days | Last 90 Days | All Articles | RSS | Hail Support


Categories:
·GNU/Linux
·Homebrew designs
·Perl
·Administration
·Backup/Recovery
·Bugs/Fixes
·Certification
·Database
·Email
·File/Print
·Hardware
·Information Grab Bag
·Interoperability
·GNU/Linux ABCs
·Monitoring
·Name Resolution
·Network Services
·Networking
·Remote Control
·Security
·Desktop
·Web
·BSD
·Solaris
·GIAGD
·REALbasic
·All Categories


Match on Field Contents with PHP/MySQL
Topic: Web   Posted:2005-10-25
Printer Friendly: Print

spacer
If you wish to search for a match within a field, rather than matching the entire field with a MySQL query, you can use LIKE. Here is an HTML page that will display an input box on a form:

<html>
<head>
<title>Search</title>
</head>
<body bgcolor="white">
<form method="POST" action="search.php">
<table>
<col span="1" align="right">
<tr>
<td><font color="blue">Search Term:</font></td>
<td><input type="text" name="searchterm" size=50></td>
</tr>
<tr>
<td><input type="submit" value="Submit"></td>
</tr>
</table>
</form>
</body>
</html>

Here is the PHP code that will take the search term and then display a different field of a record that contains the match:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=iso8859-1">
<title>Search</title>
</head>
<body bgcolor="white">
<?php
foreach($HTTP_POST_VARS as $varname => $value)
$formVars[$varname]=$value;
require_once("config.php");
$db1=mysql_connect($dbhost, $dbuname, $dbpass);
mysql_select_db($dbname);
$searchterm=$formVars["searchterm"];
$query="SELECT * FROM entry WHERE searchfield LIKE '%".$searchterm."%'";
$result = mysql_query($query);
$j=1;
while ($row = mysql_fetch_row($result)) {
print $row[3];
}
mysql_close($db1);
?>
</body>
</html>

The config.php file needs to have your real logon and database information in it. We like to make this an inclusion for security reasons. Here is an example file:

<?php
$dbhost = "host";
$dbuname = "user";
$dbpass = "password";
$dbname = "search";
?>

Here is what these two pages look when running:





This works for us, because the field we are searching on happens to be a list of keywords related to the field name. For full text searching, see this excellent article: Using MySQL Full-text Searching




Please read our Terms of Use
Microsoft, Windows, Windows XP, Windows 2003, Windows 2000, and NT are either trademarks or registered trademarks of Microsoft Corporation. NetAdminTools.com is not affiliated with Microsoft Corporation. Linux is a registered trademark of Linus Torvalds, and refers to the Linux kernel. The operating system of most distributions that contain the Linux kernel is GNU/Linux. All logos and trademarks in this site are property of their respective owner. Copyright 1997-2008 NetAdminTools.com

Created by:
MCJ
MCJ CMS