【问题标题】:mysql_fetch_array(): supplied argument is not a valid MySQL result resource [duplicate]mysql_fetch_array():提供的参数不是有效的 MySQL 结果资源 [重复]
【发布时间】:2011-11-30 14:20:07
【问题描述】:

可能重复:
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();
?>

【问题讨论】:

  • 当您进行数据库查询时,请始终在末尾添加or die( mysql_error() ),以便您查看查询中是否有错误。
  • 你已经得到了答案 - stackoverflow.com/questions/8327003/…
  • 啊,好老 "$var" cargo-cult 编程...

标签: php mysql


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

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

    由于mysql_query() 将在带有错误的SELECT 语句中返回FALSE,因此您可以执行以下操作:

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

    您会希望对 mysql_connect()mysql_db_select() 调用进行类似的错误检查,因为它们也会导致错误。

    【讨论】:

    • 谢谢!我现在得到: Parse error: syntax error, unexpected $end in /websites/123reg/LinuxPackage22/we/ez/y_/weezy.co.uk/public_html/search.php 在第 63 行,但该行只是
    猜你喜欢
    • 1970-01-01
    • 2013-07-14
    • 2013-09-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多