我正在尝试创建一个从mySQL数据库中提取信息的搜索引擎。我的代码如下:
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Search Engine Test</title>
</head>
<body>
<script language="php">
// Create a database connection
$connection = mysql_connect("localhost", "root", "", "test");
if (!connection) {
die("Please reload page. Database connection failed: " . mysql_error());
}
// Select a databse to use
$db_select = mysql_select_db("test", $connection);
if (!$db_select) {
die("Please reload page. Database selection failed: " . mysql_error());
}
// Search Engine
// Only execute when button is pressed
if (isset($_POST['keyword'])) {
// Filter
$keyword = trim($_POST['keyword']);
// Select statement
$search = "SELECT Price FROM 'table_1' WHERE * LIKE '%$keyword%'";
// Display
$result = mysql_query($search) or die('query did not work');
while ($result_arr = mysql_fetch_array($result)) {
echo $result_arr['*'];
echo " ";
echo "<br>";
echo "<br>";
}
$anymatches = mysql_num_rows($result);
if ($anymatches == 0) {
echo "Nothing was found that matched your query.<br><br>";
}
}
</script>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<input type="text" name="keyword">
<input type="submit" name="search" value="Search">
然而,当我运行它时,我收到一条通知,说我被禁止访问服务器。任何帮助将不胜感激。
答案 0 :(得分:0)
$search = "SELECT Price FROM table_1 WHERE fieldname LIKE '%$keyword%'";
您不能在字段名称周围使用单引号,因为它是如何定义字符串的。如果表名是保留字,则可以使用密钥(`)。你也不能做WHERE *,你需要指定一个字段来比较关键字。
我还建议不要在PHP中使用mysql命令,因为它们已被弃用。使用更新的mysqli命令