资源ID已返回但仍然收到警告:mysql_fetch_assoc():提供的参数不是有效的MySQL结果资源

时间:2011-12-07 18:39:26

标签: php mysql warnings

  

可能重复:
  php warning mysql_fetch_assoc

我已经尝试了我的代码,它似乎正在工作,但它正在抛出错误。

我在LAMP环境中,这是我的代码。

    <?php
    mysql_select_db($database_conn1, $conn1);

    //DROPING VIEW
    $query_rsDropView = "DROP VIEW attorneyOrder;";
    echo $query_rsDropView . "/////";  //this code outputs nothing, not even the "/////" part.
    $rsDropView = mysql_query($query_rsDropView, $conn1) or die(mysql_error());
    echo $rsDropView . "/////";     //output the value "1" (witout quotes)
    $row_rsDropView = mysql_fetch_assoc($rsDropView);      //line 222
    $totalRows_rsDropView = mysql_num_rows($rsDropView);   //line 223

    //CREATING VIEW
    $query_rsView = "CREATE VIEW attorneyOrder AS SELECT * FROM LewisJohsAttorneys ORDER BY lname ASC;";
    $rsView = mysql_query($query_rsView, $conn1) or die(mysql_error());
    echo $rsView . "/////";     //outputs the value "1" (without quotes)
    $row_rsView = mysql_fetch_assoc($rsView);      //line 228
    $totalRows_rsView = mysql_num_rows($rsView);   //line 229

    //GETTING VALUES FOR SELECT ON VIEW
    $urlStart_rsName = "NULL";
    if (isset($_GET['start'])) {
         $urlStart_rsName = $_GET['start'];
    }
    $urlEnd_rsName = "NULL";
    if (isset($_GET['end'])) {
         $urlEnd_rsName = $_GET['end'];
    }

    //SELECTING DATA FROM VIEW
    $query_rsName = sprintf("SELECT * FROM attorneyOrder WHERE attorneyOrder.lname BETWEEN %s AND %s;", GetSQLValueString($urlStart_rsName, "text"),GetSQLValueString($urlEnd_rsName, "text"));
    $rsSearch = mysql_query($query_rsName, $conn1) or die(mysql_error());
    echo $rsSearch . "/////";  //outputs a resouce id
    $totalRows_rsName = mysql_num_rows($rsName);    //line 249

    //OUTPUTING DATA
    while($rsSearch = mysql_fetch_assoc($rsSearch)){   //line 262
    //do some stuff here
    }

    ?>

我得到的错误是:

    Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /var/www/lewisjohs.com/attorneys-search.php on line 222

    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /var/www/lewisjohs.com/attorneys-search.php on line 223

    Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /var/www/lewisjohs.com/attorneys-search.php on line 228

    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /var/www/lewisjohs.com/attorneys-search.php on line 229

    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /var/www/lewisjohs.com/attorneys-search.php on line 249

    Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /var/www/lewisjohs.com/attorneys-search.php on line 262

我假设它全部来自同一个问题,这就是为什么每个连续的查询返回一个无效值,因为它们都是从前一个输出构建的。

但是,我仍然得到正确的输出。我可以压制错误,但我想知道为什么会出现这些错误。在线搜索之后,一个常见的原因是没有指定数据库,但这是我用这行做的第一件事[code] mysql_select_db($ database_conn1,$ conn1); [/ code]

我尝试输出查询的返回结果,并在代码中注释了它们的输出。它们似乎正确地返回为“1”或true,或资源ID。

1 个答案:

答案 0 :(得分:0)

你的错误是有原因的。你不需要压制它们,而是要修复它们。

从数据库中选择内容时使用

mysql_fetch_assoc()函数。在这种情况下,返回所选行的数组,这些行使用此函数获取。 第222行和第228行的错误是因为CREATE VIEW或DROP VIEW不是选择查询,并且在那里完全没有必要。 第223,229行的mysql_num_rows()也是如此。此函数也仅用于SELECT查询。 第249行出现错误,因为脚本中不存在变量$ rsName。这应该是mysql_num_rows($ rsSearch);如果你正在使用mysql_query函数执行SELECT,SHOW,DESCRIBE,EXPLAIN查询,如果传递ok则返回ResultSet,如果失败则返回FALSE。您可以使用mysql_fetch_assoc()获取Resultset并循环遍历它。但是如果该查询没有行,那么mysql_query将返回false,这不能传递给mysql_fetch_assoc()

这是应该写的:

<?php
mysql_select_db($database_conn1, $conn1);

//DROPING VIEW
$q_drop = "DROP VIEW attorneyOrder;";
echo $q_drop. "/////"; // prints out string stored in $q
$rsDropView = mysql_query($q_drop, $conn1) or die(mysql_error());
echo $rsDropView . "/////";     // prints TRUE (1) if mysql_query() went well or FALSE(0) if it failed

//CREATING VIEW
$q_view = "CREATE VIEW attorneyOrder AS SELECT * FROM LewisJohsAttorneys ORDER BY lname ASC;";
$rsView = mysql_query($q_view, $conn1) or die(mysql_error());
echo $rsView . "/////";     // prints TRUE (1) if mysql_query() went well or FALSE(0) if it failed


//GETTING VALUES FOR SELECT ON VIEW
$urlStart_rsName = "NULL";
if (isset($_GET['start'])) {
     $urlStart_rsName = $_GET['start'];
}
$urlEnd_rsName = "NULL";
if (isset($_GET['end'])) {
     $urlEnd_rsName = $_GET['end'];
}

//SELECTING DATA FROM VIEW
$query_rsName = sprintf("SELECT * FROM attorneyOrder WHERE attorneyOrder.lname BETWEEN %s AND %s;", GetSQLValueString($urlStart_rsName, "text"),GetSQLValueString($urlEnd_rsName, "text"));
$rsSearch = mysql_query($query_rsName, $conn1) or die(mysql_error());
echo $rsSearch . "/////";  //outputs a resouce id
$totalRows_rsName = mysql_num_rows($rsSearch);    //line 249

//OUTPUTING DATA
if($totalRows_rsName>0){
    while($rsSearch = mysql_fetch_assoc($rsSearch)){   //line 262
        //do some stuff here
    }
}


?>