<?
/*
只需调用 searchtable() 函数
使用这些变量,它将搜索所需的
数据库并为每个字段生成一个表,
其中有一个匹配项。
*/
function searchtable($host,$user,$pass,$database,$tablename,$userquery)
{
$link = mysql_connect($host, $user, $pass) or die("Could not connect: " . mysql_error());
$db = mysql_select_db($database, $link) or die(mysql_error());
$fields = mysql_list_fields($database, $tablename, $link);
$cols = mysql_num_fields($fields);
for ($i = 1; $i < $cols; $i++) {
$allfields[] = mysql_field_name($fields, $i);
}
foreach ($allfields as $myfield) {
$result = mysql_query("SELECT * FROM $tablename WHERE $myfield like '%$userquery%' ");
if (mysql_num_rows($result) > 0){
echo "<h3>search <i>$database</i> for <i>$userquery</i>, found match(es) in <i>$myfield</i>: </h3>\n";
echo "<table border=1 align=\"center\">\n\t<tr>\n";
for ($i = 1; $i < $cols; $i++) {
echo "\t\t<th";
if ($myfield == mysql_field_name($fields, $i)){
echo " bgcolor=\"orange\"> ";
} else {
echo ">";
}
echo mysql_field_name($fields, $i) . "</th>\n";
}
echo "\t</tr>\n";
$myrow = mysql_fetch_array($result);
do {
echo "\t<tr>\n";
for ($i = 1; $i < $cols; $i++){
echo "\t\t<td> $myrow[$i] </td>\n";
}
echo "\t</tr>\n";
} while ($myrow = mysql_fetch_array($result));
echo "</table>\n";
}
}
}
searchtable($host,$user,$pass,$database,$tablename,$userquery);
?>