-1

The below code is meant to be shown the total number of members but it doesn't show nothing

<?php
            $query = $forumdb->prepare("SELECT totalMembers FROM smf_settings");
            $query->execute();
            $num_rows = $query->fetch();
            ?>
            <div class="media-body">
            <p id="greet" align="left">Total Members: <?php echo .$num_rows['totalMembers']. ?> <p>
            </div>
David Dejan
  • 29
  • 1
  • 6
  • @SubinThomas `$num_rows` is just a variable calling the `fetch()` function. `$num_rows` does not mean `num_rows()`. – Funk Forty Niner Sep 19 '15 at 13:08
  • Seeing your other questions, and using `mysql_` functions, is really hard to say which MySQL API you're using to connect with. Voted to close as unclear. – Funk Forty Niner Sep 19 '15 at 13:10
  • My bad.. Thanks.. @Fred-ii- – Subin Thomas Sep 19 '15 at 13:11
  • possible exact duplicate of http://stackoverflow.com/questions/32667030/expects-parameter-warning-1-to-be-resource – Funk Forty Niner Sep 19 '15 at 13:11
  • I'm using PDO @Fred-ii- – David Dejan Sep 19 '15 at 13:29
  • 1
    you need to post your full code. Check for errors http://php.net/manual/en/pdo.error-handling.php --- http://php.net/manual/en/function.error-reporting.php or see the answer given below. – Funk Forty Niner Sep 19 '15 at 13:32
  • you also shouldn't be reposting the same question. I don't mean to sound "mean" here, but you would benefit more from reading the right tutorials. Your questions have been one after another using different MySQL APIs/functions and hoping something will eventually work. – Funk Forty Niner Sep 19 '15 at 13:38
  • its been awhile i'm behind this honestly, nothing worked so far. But one thing i noticed is, the output shows 1 even tho it is not. That was the same problem in the previous question i asked http://stackoverflow.com/questions/32666137/few-warnings-expects-parameter, i followed it and worked but this one is really driving me crazy – David Dejan Sep 19 '15 at 13:44
  • 1
    Here, the answer to all your problems http://www.mysqltutorial.org/php-querying-data-from-mysql-table/ - Follow that to a **"T".** - Including any links in there. This comment should be converted to an answer ;-) – Funk Forty Niner Sep 19 '15 at 13:52

1 Answers1

1

To count total number of members you can use count

$query->execute();
$rows = $query->fetch();
$num_rows = count($rows);

OR change your query to count and use fetchColumn

$query = $forumdb->prepare("SELECT COUNT(totalMembers) FROM smf_settings");
$query->execute();
/* Check the number of rows that match the SELECT statement */
$num_rows=$res->fetchColumn();
Saty
  • 22,443
  • 7
  • 33
  • 51
  • 1
    For most databases, PDOStatement::rowCount() does not return the number of rows affected by a SELECT statement. Instead, use PDO::query() to issue a SELECT COUNT(*) statement with the same predicates as your intended SELECT statement, then use PDOStatement::fetchColumn() to retrieve the number of rows that will be returned. Your application can then perform the correct action. – Subin Thomas Sep 19 '15 at 12:31
  • You are right @SubinThomas thanks for updating my knowledge – Saty Sep 19 '15 at 12:33
  • What do you mean by does't work??Have your get and error and it is better for you to post your full code – Saty Sep 19 '15 at 13:51
  • `prepare("SELECT totalMembers FROM smf_settings"); $query->execute(); $rows = $query->fetch(); $num_rows = count($rows); ?>

    Total Members:

    `
    – David Dejan Sep 19 '15 at 13:57
  • @Saty Unclear questions always take 10 times as long than... well, you know ;-) – Funk Forty Niner Sep 19 '15 at 14:12