警告:mysql_fetch_assoc

时间:2015-09-17 14:14:43

标签: php mysql

我收到以下错误:

  

警告:mysql_fetch_assoc()期望参数1是资源,   第19行/home/inv/public_html/set.php中给出的布尔值

第19行是:

    $row = mysql_fetch_assoc($result);

完整代码:

<?php

$sitename = "ASDASDCSGO";

$link = mysql_connect("localhost", "x", "y");

$db_selected = mysql_select_db('x', $link);

mysql_query("SET NAMES utf8");



function fetchinfo($rowname,$tablename,$finder,$findervalue) {

    if($finder == "1") $result = mysql_query("SELECT $rowname FROM $tablename");

    else $result = mysql_query("SELECT $rowname FROM $tablename WHERE `$finder`='$findervalue'");

    $row = mysql_fetch_assoc($result);

    return $row[$rowname];

}

?>

2 个答案:

答案 0 :(得分:0)

生成此警告是因为$ result为null。 检查你的结果是否是布尔值。

    <?php

$sitename = "ASDASDCSGO";

$link = mysql_connect("localhost", "x", "y");

$db_selected = mysql_select_db('x', $link);

mysql_query("SET NAMES utf8");



function fetchinfo($rowname,$tablename,$finder,$findervalue) {
     // you can't access php variable as a string in query
    if($finder == "1") $result = mysql_query("SELECT '.$rowname.' FROM '.$tablename.'");

    else $result = mysql_query("SELECT '.$rowname.' FROM '.$tablename.' WHERE '.$finder.'='.$findervalue.'");

    if(!is_bool($result)){ // this is for checking your $result varible is boolean or not
    $row = mysql_fetch_assoc($result);
    }else{
    echo "Error";
    }

    return $row[$rowname];

}

?>

答案 1 :(得分:0)

<?php
function fetchinfo($rowname,$tablename,$finder,$findervalue) {

    if($finder == "1") $result = mysql_query("SELECT $rowname FROM $tablename");

    else $result = mysql_query("SELECT ".$rowname." FROM ".$tablename." WHERE ".$finder."=".$findervalue."");

    $row = mysql_fetch_assoc($result);

    return $row[$rowname];

}

或者您可以像这样使用您的查询

mysql_query('SELECT {$rowname} FROM {$tablename} WHERE {$finder}={findervalue}');