-2

I've successfully written a search engine in PHP,

After testing it, there's one thing that bothers me.

First error

(mysql_fetch_array() expects parameter 1 to be resource, boolean given in)  

Second error

(mysql_num_rows() expects parameter 1 to be resource, boolean given in)  

Sorry, but we can not find an entry to match your query...

here's the code

<?php

echo "<h2>Search Results:</h2><p>";

//If they did not enter a search term we give them an error
if ($find == "Account_Number")
{
echo "<p>You forgot to enter a search term!!!";
exit;
}

// Otherwise we connect to our Database
mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());

// We perform a bit of filtering
$find = strtoupper($find);
$find = strip_tags($find);
$find = trim ($find);

//Now we search for our search term, in the field the user specified
$data = mysql_query("SELECT * FROM memaccounts WHERE upper($field) LIKE'%$find%'");

//And we display the results
while($result = mysql_fetch_array( $data ))
{
echo $result['Account_Number'];
echo $result['Name'];
echo "<br>";
echo $result['Balance'];
echo "<br>";
echo "<br>";
}

$anymatches=mysql_num_rows($data);
if ($anymatches == 0)
{
echo "Sorry, but we can not find an entry to match your query...<br><br>";
}

//And we remind them what they searched for
echo "<b>Searched For:</b> " .$find;
//}
?>

please help, Thank you

4

5 回答 5

1

如果有 SQL 错误,mysql_query() 返回 false。在遍历结果之前,您必须检查 $data 是否为假。一个例子:

if (!$data) exit(mysql_error());

或者只是在 mysql_query() 行的末尾添加“or die(mysql_error())”,方法与使用 connect 和 select_db 的方式相同。

于 2013-07-24T07:31:08.317 回答
0

您的查询可能有错误。检查它上面没有任何错字。
我建议捕获查询(回显它或记录它),然后使用任何数据库客户端(如 phpmyadmin 或 heidisql)运行它。

于 2013-07-24T07:35:41.593 回答
0

查询可能会起作用,除非$find包含无效字符。例如,如果它包含引号 ( '),那么它将破坏您的查询。

您可以使用mysql_real_escape_string它来解决这个问题,尽管最好完全退出使用 mysql* 函数并切换到 PDO。不推荐使用 mysql* 函数,并且可能会在 PHP 的未来版本中删除。

于 2013-07-24T07:37:00.213 回答
0

由于$findand$field没有定义,查询变为:

SELECT * FROM memaccounts WHERE upper() LIKE'%%'

这使得mysql_query()return FALSE,因为它是一个不正确的查询。(即$dataFALSE)。因此,mysql_fetch_array()并且作为输入提供mysql_num_rows()的错误失败。FALSE

提示:

  • 要正确过滤输入,请使用mysqli_real_escape_string(). 顺便说一句,如果您的表格排序不区分大小写,则无需将其更改为大写。

  • 您始终可以通过、 或var_dump()var_export()视情况而定)调试您的代码echoprint_r()

  • 停止使用已弃用mysql_*的功能。请改用 MySQLi 或 PDO。

  • 您的代码受到 SQL 注入攻击,因为您直接允许在查询中插入 POST 值。

于 2013-07-24T07:37:29.053 回答
0
 Please check the query.
mysql_query() this function returns true or false to show query worked or not.
your result shows boolean means query failed.
use mysql_error() 
for ex :- 
$result=mysql_query('Your query');
if(!$res){
 die(mysql_error()); //gives information regarding error
}
于 2013-07-24T07:48:00.440 回答