PHP simple search engine getting error: Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given [duplicate]

陌路散爱 提交于 2019-12-13 09:50:04

问题


I keep getting this error when i click the search button:

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\testsearch.php on line 25

here is the code:

<form method="get" action="">
<label>Search For: </label><input type="text" name="query" />
<input type="submit" name="submit" value="Start Search" />
<input type="reset" value="Reset"
</form>

php code starts here

if(isset($_GET['submit'])){
// Change the fields below as per the requirements`enter code here`
$db_host="localhost";
$db_username="root";
$db_password="";
$db_name="cosmetics";
$db_tb_name="makeup";
$db_tb_atr_name="";

//Now we are going to write a script that will do search task
// leave the below fields as it is except while loop, which will display results on      screen

mysql_connect("$db_host","$db_username","$db_password");
mysql_select_db("$db_name");
$query=mysql_real_escape_string($_GET['query']);
$query_for_result=mysql_query("SELECT * FROM $db_tb_name WHERE
$db_tb_atr_name like '%".$query."%'");
echo "<h2>Search Results</h2><ol>";
while($data_fetch=mysql_fetch_array($query_for_result)) 
{
echo "<li>";
echo substr($data_fetch[$db_tb_atr_name], 0,160);
echo "</li><hr/>";
}
echo "</ol>";
mysql_close();
}
?>

回答1:


Your $db_tb_atr_name variable is empty. That makes your SELECT statement invalid and your mysql_query fails.

Initialize variable with a name of a column you're looking for values in:

$db_tb_atr_name="column1";
                 ^^^^^^^^



回答2:


$db_tb_atr_name is empty. Define it!



来源:https://stackoverflow.com/questions/16266880/php-simple-search-engine-getting-error-warning-mysql-fetch-array-expects-par

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!