调用mysql_fetch_array()时获取“提供的参数不是有效的MySQL结果资源”

时间:2011-11-30 13:52:58

标签: php mysql

我一直得到相同的mysql错误代码,但我不知道如何纠正它。

错误:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /websites/123reg/LinuxPackage22/we/ez/y_/weezy.co.uk/public_html/search.php on line 29

我在下面的代码中标出了第29行。

这是什么意思?

谢谢

<?php
// Change the fields below as per the requirements
$db_host="";
$db_username="";
$db_password="";
$db_name="";
$db_tb_name="data";
$db_tb_atr_name="name";
$db_tb_atr_name="email";
$db_tb_atr_name="location";


//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 name, email, location

FROM data WHERE 

$db_tb_atr_name like '%".$query."%'");
echo "<h2>Search Results</h2><ol>";
while ($row = mysql_fetch_array($result))  <<<<<<<<<<<<<<< LINE 29 
{
echo "<li>";
echo substr($row["name"], $row["email"], $row["location"]);
echo "</li><hr/>";
}
echo "</ol>";

mysql_close();
?>

4 个答案:

答案 0 :(得分:6)

while ($row = mysql_fetch_array($result)) 

您将$result设置为何处?

你可能意味着

while ($row = mysql_fetch_array($query_for_result))

答案 1 :(得分:1)

您将查询结果存储在名为$query_for_result的变量中,稍后您尝试阅读$result,这是空的。

答案 2 :(得分:1)

您要将mysql_query的结果分配给$query_for_result,然后尝试在$result中循环mysql_fetch_array()。试试这个:

$result = mysql_query("...");
while ($row = mysql_fetch_array($result)) {
  ...

OR

$query_for_result = mysql_query("...");
while ($row = mysql_fetch_array($query_for_result)) {
  ...

答案 3 :(得分:0)

您还应该检查是否有任何行返回

$rs = mysql_query("
  SELECT name, email, location
    FROM data 
   WHERE ".$db_tb_atr_name." like '%".$query."%'
");

if(mysql_num_rows($rs)){

  while ($row = mysql_fetch_array($rs)){
    // code
  }
} else {
// no results
}