-1

This below code is giving me mysql_fetch_array() expects parameter

<?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']);
?>

Warning: mysql_fetch_array() expects parameter 1 to be resource, object given in /home/gamin1/public_html/ucp/forumstats.php on line 127

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
David Dejan
  • 29
  • 1
  • 6

2 Answers2

1

Seems to be a reasonable assumption that you want to use PDO. As has already been mentioned, you can't mix pdo(_msyql) with the other mysql apis.
But there are some other issues with your script:

<?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'];
}
VolkerK
  • 95,432
  • 20
  • 163
  • 226
  • OP reposted http://stackoverflow.com/questions/32668158/query-fetching-issue and used one of the answers in their new code. I voted to close the other one as unclear, since we have no idea which MySQL API they're using to connect with. Edit: My guess http://stackoverflow.com/questions/32666137/few-warnings-expects-parameter they're still using mysql_ to connect with. – Funk Forty Niner Sep 19 '15 at 13:12
  • Exactly. I think/feel the OP needs a good jolt with tutorials. They're not far off though ;-) – Funk Forty Niner Sep 19 '15 at 15:29
0

Use $query->fetch() instead of mysql_fetch_array($query).

You are using PDO here: http://php.net/manual/fr/pdostatement.fetch.php

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360