php 简单的php/sql搜索引擎

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17892260/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:38:29  来源:igfitidea点击:

Simple php/sql search engine

phpsqlsearchsearch-engine

提问by Tim

I am trying to make a simple php, sql search engine that will select everything from my database that is "LIKE" the keyword (query) and display it. However it will not work. It only displays the text "problem"(see line 32) and after hours of troubleshooting I still can not figure this out.

我正在尝试制作一个简单的 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>

回答by ?_?

Expanding your code, we can see that:

展开你的代码,我们可以看到:

$result = @mysql_query($search);

turns into:

变成:

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

Which doesn't make much sense.

这没有多大意义。

Change the first line to:

将第一行更改为:

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

or, instead of assigning $searcha value at all, just skip to assigning $resultthe value that $searchcurrently has.

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

EDIT: to help you explain:

编辑:帮助您解释:

Change this:

改变这个:

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

to this:

对此:

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

回答by bestprogrammerintheworld

Try and change:

尝试改变:

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();
}

to:

到:

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');

IMPORTANT!<script language="php">isn't valid. You should type <?phpat the beginning of php-code and ?>to end php-code.

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

UPDATE:You will also have to change this 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>"; 
 }
}

TO:

到:

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>"; 
}
}

When making new code, you really should NOT use mysql_functions*, because they're deprecated. Look into PDO or mysqli instead.

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