<!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 an entire database with PHP</title>
<style type="text/css">
table {
width: 100%;
border-collapse:collapse;
}
table, th, td {
border: 1px solid black;
padding: 3px;
}
a { color: #4D2078; PADDING-RIGHT: 2px; PADDING-LEFT: 2px; PADDING-BOTTOM: 2px; PADDING-TOP: 2px; background-color:#EEEBFF; height: 20px; width: 120px; text-align: center; ; border: #A498BD; border-style: outset; border-top-width: 2px; border-right-width: 2px; border-bottom-width: 2px; border-left-width: 2px}
a:hover { BORDER-RIGHT: ##605080 1px outset; PADDING-RIGHT: 2px; BORDER-TOP: #605080 1px outset; PADDING-LEFT: 2px; PADDING-BOTTOM: 2px; BORDER-LEFT: #605080 1px outset; PADDING-TOP: 2px; BORDER-BOTTOM: #605080 1px outset;background-color:#BDAAE2; height: 20px; width: 120px; text-align: center; }
</style>
</head>
<body>
<?php if (isset($_POST["search"]) && ($_POST["search"] != "")) { ?>
<a href="searchdb.php">Search Again !!</a>
<br>
<?php } ?>
<?php if (isset($_POST["search"]) && ($_POST["search"] != "")) {
$ori_search=$_POST["search"];
$highlight_search="<font color=\"red\">" . $_POST["search"] . "</font>";
$conn = mysql_pconnect("localhost", $_POST["username"], $_POST["password"]) or die ("Error connecting to mysql");
//----------------mysql utf8----------------
mb_internal_encoding('utf8');
mysql_query("SET CHARACTER SET 'utf8'");
mysql_query("SET NAMES 'UTF8;'");
mysql_query("SET CHARACTER_SET_CLIENT=UTF8;");
mysql_query("SET CHARACTER_SET_RESULTS=UTF8;");
//----------------mysql utf8----------------
mysql_select_db($_POST["database"]);
$record_exists="false";
$aryTables = array();
$aryFields = array();
$sql = "SHOW TABLES FROM " . $_POST["database"];
$result = mysql_query($sql);
while ($row = mysql_fetch_row($result)) {
$aryTables[sizeof($aryTables)] = $row[0];
}
for ($i = 0; $i < sizeof($aryTables); $i = $i + 1) {
$sql = "SHOW COLUMNS FROM " . $aryTables[$i];
$result = mysql_query($sql);
while ($row = mysql_fetch_row($result)) {
$aryFields[sizeof($aryFields)] = $row[0];
}
$sql = "SELECT * FROM " . $aryTables[$i] . " WHERE ";
for ($j = 0; $j < sizeof($aryFields); $j = $j + 1) {
$sql = $sql . $aryTables[$i] . "." . $aryFields[$j] . " LIKE '%" . $_POST["search"] . "%'";
if (($j + 1) != sizeof($aryFields)) {
$sql = $sql . " OR ";
} else {
$sql = $sql . ";";
}
}
$result = mysql_query($sql);
if (mysql_num_rows($result) > 0) {
echo "<p>" . $aryTables[$i] . "</p>";
echo "<table><tr><thead>";
foreach ($aryFields as $field => $value) {
echo "<th>" . $value . "</th>";
}
while ($aryData = mysql_fetch_assoc($result)) {
echo "<tr>";
for ($j = 0; $j < sizeof($aryFields); $j = $j + 1) {
echo "<td>" . str_replace($ori_search,$highlight_search,substr(htmlspecialchars($aryData[$aryFields[$j]], ENT_QUOTES), 0, 150)) . "</td>";
}
echo "</tr>";
}
echo "</table>";
$record_exists="true";
}
$aryFields = array();
}
if ($record_exists == "false"){
header("location:searchdb.php?record_exists=1");
}
} else {?>
<?php
if (isset($_GET['record_exists']) && $_GET['record_exists'] == "1"){
echo "<font color=\"red\">No Record Found !</font>";
}
?>
<form name="details" action="searchdb.php" method="POST">
<p>Database: <input type="text" name="database" value="DBF2mySQL" /></p>
<p>Username: <input type="text" name="username" value="root" /></p>
<p>Password: <input type="password" name="password" value="charlie" /></p>
<p>Search term: <input type="text" name="search" /></p>
<p><input type="submit" value="Search" /> <input type="reset" value="Reset" /></p>
</form>
<?php } ?>
<?php if (isset($conn)) {mysql_close($conn);} ?>
</body>
</html>
No comments:
Post a Comment