问题
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