嗨,這是我的腳本,它很適合搜索我的數據庫。 我有一個form.html用戶在搜索框中輸入關鍵字。突出搜索文本在mysql php搜索
<form method="get" action="form.php">
<input type="text" name="searchterm" title="Enter your search term here" value="Enter your search term here" class="searchbox" />
<input type="submit" name="search" title="Search Now! "value="Search" class="searchbutton" />
</form>
我想突出顯示結果中的搜索文本。
千恩萬謝
form.php的
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Search</title>
<link href="default.css" rel="stylesheet" type="text/css" media="screen" />
</head>
<body>
<?php
// include MySQL-processing classes
require_once 'mysql.php';
try{
// connect to MySQL
$db=new MySQL(array('host'=>'','user'=>'','password'=>'','database'=>''));
$searchterm=$db->escapeString($_GET['searchterm']);
$result=$db->query("SELECT * FROM BookPage WHERE (PageText) like \"%$searchterm%\" ");
if(!$result->countRows()){
echo '<div class="maincontainer"><h2>No results were found. Go back and try a new search.</h2></div>'."n";
}
else{
// display search results
echo '<div class="maincontainer"><h2>Your search criteria
returned '.$result->countRows().' results.</h2>'."n";
while($row=$result->fetchRow()){
echo '<div class="rowcontainer"><p><strong>Book Id:
</strong>'.$row['BookId'].'<p><p><strong>Page Id:
</strong>'.$row['PageId'].'</p><p><strong>Page Text:
</strong>'.$row['PageText'].'</p></div>'."n";
}
}
echo '</div>';
}
catch(Exception $e){
echo $e->getMessage();
exit();
}
?>
</body>
</html>
感謝了很多行之有效 – jk1844