0

我正在尝试制作一个简单的 php、sql 搜索引擎,它将从我的数据库中选择“喜欢”关键字(查询)的所有内容并显示它。但是它不会起作用。它只显示文本“问题”(见第 32 行),经过数小时的故障排除,我仍然无法弄清楚这一点。

<!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("*****","*****","*****");   
if (!connection) {
    die ("Please reload page. Database connection failed: " . mysql_error());
}

// Select a databse to use
$db_select = mysql_select_db("*****",$connection);
if (!$db_select) {
    die("Please reload page. Database selection failed: " . mysql_error());
}

// Search Engine
// Only execute when button is pressed
if (isset($_POST['search'])) {
// Filter
//$keyword = trim ($keyword);
echo $keyword;
// Select statement
$search = mysql_query("SELECT * FROM tbl_name WHERE cause_name LIKE '%keyword%'");
// Display
$result = @mysql_query($search);
if (!$result){
echo "problem";
exit();
}


while($result = mysql_fetch_array( $search )) 
 { 
 echo $result['cause_name']; 
 echo " ";
 echo "<br>"; 
 echo "<br>"; 
 }
 $anymatches=mysql_num_rows($search); 
 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">

</body>
</html>
4

2 回答 2

1

尝试并改变:

if (isset($_POST['search'])) { //$_POST['search'] just tells that there are a submit-button when submitting (and the name of it)
// Filter
//$keyword = trim ($keyword);
echo $keyword; //You're echoing out value of $keyword which hasn't been set/assigned
// Select statement

//You're always searching for the word keyword with leading and/or trailing characters
//You're not searching for a dynamically assigned value which I think is what you want
$search = mysql_query("SELECT * FROM tbl_name WHERE cause_name LIKE '%keyword%'");

//You're executing an already defined query (assigned in $search)
$result = @mysql_query($search); //You're suppressing errors, it's bad practice.
if (!$result){
echo "problem";
exit();
}

至:

if (isset($_POST['keyword'])) {
// Filter
$keyword = trim ($_POST['keyword']);

// Select statement
$search = "SELECT * FROM tbl_name WHERE cause_name LIKE '%$keyword%'";
// Display
$result = mysql_query($search) or die('query did not work');

重要的! <script language="php">无效。您应该<?php在 php-code 的开头键入并?>结束 php-code。

更新: 您还必须更改此代码:

while($result = mysql_fetch_array( $search )) 
    { 
    echo $result['cause_name']; 
    echo " ";
    echo "<br>"; 
    echo "<br>"; 
 }
 $anymatches=mysql_num_rows($search); 
 if ($anymatches == 0) 
 { 
     echo "Nothing was found that matched your query.<br><br>"; 
 }
}

至:

while($result_arr = mysql_fetch_array( $result )) 
{ 
echo $result_arr['cause_name']; 
echo " ";
echo "<br>"; 
echo "<br>"; 
}
$anymatches=mysql_num_rows($result); 
if ($anymatches == 0) 
{ 
   echo "Nothing was found that matched your query.<br><br>"; 
}
}

在编写新代码时,您真的不应该使用 mysql_ functions*,因为它们已被弃用。改为查看 PDO 或 mysqli。

于 2013-07-27T00:08:43.947 回答
1

扩展您的代码,我们可以看到:

$result = @mysql_query($search);

变成:

$result = @mysql_query(mysql_query("SELECT * FROM tbl_name WHERE cause_name LIKE '%keyword%'"));

这没有多大意义。

将第一行更改为:

$search = "SELECT * FROM tbl_name WHERE cause_name LIKE '%keyword%'"

或者,$search根本不分配值,而只是跳到分配当前具有$result的值。$search

编辑:帮助您解释:

改变这个:

$search = mysql_query("SELECT * FROM tbl_name WHERE cause_name LIKE '%keyword%'");
// Display
$result = @mysql_query($search);
if (!$result){
    echo "problem";
    exit();
}

对此:

$result = mysql_query("SELECT * FROM tbl_name WHERE cause_name LIKE '%keyword%'");
// Display
if (!$result){
    echo "problem";
    exit();
}
于 2013-07-26T22:55:37.837 回答