期望参数警告1是资源

时间:2015-09-19 10:18:15

标签: php

以下代码为我提供mysql_fetch_array()期望参数

<?php
    $query = $forumdb->prepare("SELECT COUNT(*) as cnt FROM smf_personal_messages");
    $query->execute();
    $num_rows = mysql_fetch_array($query);

    if ($query->rowCount() == 0) {
        echo "<tr><td colspan='6'><small>No rows found</small></td></tr>";
    }

    echo ($num_rows['cnt']);
?>
  

警告:mysql_fetch_array()期望参数1是资源,   在/home/gamin1/public_html/ucp/forumstats.php中给出的对象   127

2 个答案:

答案 0 :(得分:1)

似乎是您想要使用PDO的合理假设。如前所述,您无法将pdo(_msyql)other mysql apis混合 但是您的脚本还存在一些其他问题:

<?php
// not an error per se, but:
// no placeholders in the query + sent only once -> prepared statement superfluous
$result = $forumdb->query("SELECT COUNT(*) as cnt FROM smf_personal_messages");
// assuming PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION, so no error handling here....

// Count(*) with no explicit grouping always returns exactly one record, so
// ->rowCount() will do you no good
// instead fetch that one record and read the field holding the value of Count(*) as cnt
$row = $result->fetch();
if ( 0==$row['cnt'] ) {
    echo "<tr><td colspan='6'><small>table cotains no records</small></td></tr>";
}
else {
    echo '# of rows: ', row['cnt'];
}

答案 1 :(得分:0)

使用$query->fetch()代替mysql_fetch_array($query)

您在此处使用PDO:http://php.net/manual/fr/pdostatement.fetch.php