Searching Php

function searchtable($database,$tablename,$userquery,$host,$user,$pass){
$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);
$content = "";
for ($i = 0; $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){
$content .= "

search $database for $userquery, found match(es) in $myfield:

\n";
$content .= "\n\t\n";
for ($i = 0; $i < $cols; $i++) {
$content .= "\t\t if ($myfield == mysql_field_name($fields, $i)){
$content .= " bgcolor=\"orange\" ";
}
$content .= ">" . mysql_field_name($fields, $i) . "\n";
}
$content .= "\t
\n";
$myrow = mysql_fetch_array($result);
do {
$content .= "\t\n";
for ($i = 0; $i < $cols; $i++){
$content .= "\t\t if (preg_match("/$userquery/i", $myrow[$i])){
$mysplit = preg_split("/($userquery)/i", $myrow[$i], -1,PREG_SPLIT_DELIM_CAPTURE);
$content .= " bgcolor=\"orange\">";
for ($si=0;$si if (preg_match("/$userquery/i", $mysplit[$si])){
$content .= "$mysplit[$si]";
}else{
$content .= "$mysplit[$si]";
}
}
} else {
$content .= ">$myrow[$i]";
}
$content .= " \n";
}
$content .= "\t
\n";
} while ($myrow = mysql_fetch_array($result));
$content .= "
\n";
}
}
mysql_close($link);
return $content;
}
$page_content = searchtable(yourdb,your table,$userquery,yourhost,youruser,yourpass);
echo $page_content;
?>
//Database used as demo on my site
#
# Table structure for table `thistest`
#
CREATE TABLE thistest (
firstname varchar(20) NOT NULL default '',
lastname varchar(20) NOT NULL default '',
phone varchar(20) NOT NULL default '',
city varchar(20) NOT NULL default ''
) TYPE=MyISAM;