mysql_fetch_array():提供的参数不是有效的MySQL结果资源

时间:2011-11-30 14:20:07

标签: php mysql

  

可能重复:
  supplied argument is not a valid MySQL result resource

我正在尝试在我的网站上显示我的数据库的结果,但我一直收到错误消息或没有任何显示。

此刻的错误讯息是:

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

这是我上一个问题的更新版本,因为我编辑了php,但它仍然无效。我做错了什么?

詹姆斯

<?php
// Change the fields below as per the requirements
$db_host="";
$db_username="u2";
$db_password="";
$db_name="";
$db_tb_name="";
$db_tb_atr_name[0]="name";
$db_tb_atr_name[1]="email";
$db_tb_atr_name[2]="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_for_result=mysql_real_escape_string($_GET['query']);

$query_for_result=mysql_query("

SELECT $db_tb_atr_name[0], $db_tb_atr_name[1], $db_tb_atr_name[2]

FROM $db_tb_name WHERE 

$db_tb_atr_name[0], $db_tb_atr_name[1], $db_tb_atr_name[2] LIKE '%".$query."%'");
echo "<h2>Search Results</h2><ol>";
while ($data_fetch = mysql_fetch_array($query_for_result))
{
echo '<br>';
echo '<br>';
echo '<div class="data1">';
echo $data_fetch["name"];
echo '</div>';
echo '<br>';
echo '<div class="data2">';
echo $data_fetch["email"];
echo '</div>';
echo '<br>';
echo '<div class="data3">';
echo $data_fetch["location"];
echo '</div>';
}
echo "</ol>";

mysql_close();
?>

2 个答案:

答案 0 :(得分:0)

运行可能从数据库中获取错误的mysql_ *命令后,您可以检查mysql_error()的结果。在这种情况下,它会告诉您mysql_query()出了什么问题。

http://php.net/manual/en/function.mysql-error.php

由于mysql_query()会在FALSE语句中返回SELECT并显示错误,您可以执行以下操作:

$query  = "SELECT ... ";
$result = mysql_query($query);

if ($result === FALSE) {
    trigger_error(mysql_error()." in ".$query);
    exit();
}

您希望对mysql_connect()mysql_db_select()来电进行类似的错误检查,因为这些错误也会导致错误。

答案 1 :(得分:0)

您可能需要设置$db_host = "localhost"$db_name = "YOUR DATABASE NAME"