phpFrmSearch.php
<html>
<head>
<title>Search Engine</title>
<style type="text/css">
// < ! - -
.Small { font-family: Arial, Helvetica, sans-serif; font-size: 10pt; text-decoration: none }
/ / - ->
</style>
</head>
<body bgcolor="#CECECE">
<form action="phpSearch.php" method="post">
ค้นหา : <input type="text" name="wKeyword" size="20">
<input type="submit" name="submit" value="ค้นหา">
</form>
</body>
</html>
phpSearch.php
<html>
<head>
<title>Search Engine</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
// < ! --
a:link { font-family: Arial, Helvetica, sans-serif; font-size: 10pt; text-decoration: none; color: blue }
a:visited { font-family: Arial, Helvetica, sans-serif; font-size: 10pt; text-decoration: none; color: blue }
a:hover { font-family: Arial, Helvetica, sans-serif; font-size: 10pt; text-decoration: none; color: red }
a:active { font-family: Arial, Helvetica, sans-serif; font-size: 10pt; text-decoration: none; color: red }
.Small { font-family: Arial, Helvetica, sans-serif; font-size: 10pt; text-decoration: none }
.Big { font-family: Arial, Helvetica, sans-serif; font-size: 13pt; text-decoration: none }
// - - >
</style>
</head>
<body bgcolor="#CECECE">
<font class="Big"><b>ผลการค้นหา คำว่า "<?php echo $wKeyword; ?>" :</b></font>
<hr>
<?php
/* ตรวจสอบตัวแปร $wKeyword ว่าเป็นค่าว่างหรือไม่ */
if ( $wKeyword == "" )
echo "โปรดระบุคำที่ต้องการสืบค้น";
else
{
/* เรียกแฟ้มข้อมูล phpConfig.php มาใช้งาน */
include( "phpConfig.php" );
/* สร้าง Connection ติดต่อกับ MySQL Server เก็บไว้กับตัวแปร $conn */
$conn = mysql_connect( $ServerName, $UserName, $UserPassword );
if ( ! $conn )
die( "ไม่สามารถติดต่อกับ MySQL ได้" );
mysql_query( "SET NAMES utf8", $conn );
/* เลือกใช้งานฐานข้อมูลที่กำหนดไว้กับตัวแปร $search */
mysql_select_db( $search, $conn )
or die ( "ไม่สามารถเลือกใช้งานฐานข้อมูล $search ได้" );
$strSQL = "SELECT * FROM tbl_web ";
$strSQL .= "WHERE wName LIKE '%$wKeyword%' ";
$result = mysql_query( $strSQL );
$num = mysql_num_rows( $result );
while ( $rs = mysql_fetch_array( $result ) )
{
$wName = eregi_replace( $wKeyword , "<b>\\0</b>", $rs[wName] );
echo "<p class=\"Small\">$wName<br>\n";
echo "<a href=\"$rs[wURL]\">$rs[wURL]</a><br>\n";
echo "$rs[wNote]<br>\n";
}
echo "<br><hr><font class=\"Small\">รวมทั้งหมด <b>$num</b> เว็บไซต์</font>";
mysql_close( $conn );
}
?>
</body>
</html>
phpConfig.php
<?php
$ServerName = "localhost";
$UserName = "root";
$UserPassword = "root";
$search = "search";
?>
CREATE TABLE `tbl_web` (
`wID` int(5) NOT NULL auto_increment,
`wName` varchar(100) character set utf8 NOT NULL default '',
`wURL` varchar(100) character set utf8 NOT NULL default '',
`wNote` longtext character set utf8 NOT NULL,
PRIMARY KEY (`wID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;
ข้มูลในตาราง
--
-- dump ตาราง `tbl_web`
--
INSERT INTO `tbl_web` VALUES (1, 'มหาวิทยาลัยศิปกร', 'http://www.su.ac.th/

INSERT INTO `tbl_web` VALUES (2, 'php.NET', 'http://www.php.net/

INSERT INTO `tbl_web` VALUES (3, 'Success Media', 'http://www.successmedia.com/

INSERT INTO `tbl_web` VALUES (4, 'มหาลัยวิทยาลัยศรีปทุม', 'http://www.spu.ac.th/

INSERT INTO `tbl_web` VALUES (5, 'Internet Thailand', 'http://www.inet.co.th

INSERT INTO `tbl_web` VALUES (6, 'Thai Farmer Bank', 'http://www.tfb.co.th

INSERT INTO `tbl_web` VALUES (7, 'Siam Commercial Bank', 'http://www.scb.co.th
