2

I am using PDO and trying to display how many users are in a table, however I get this error:

Notice: Array to string conversion in C:\Users\admin.phtml on line 10 Array

Then in my Admin class:

 public function CountUsers()
  {
    $query = "SELECT * FROM Users";

    $statement = $this->_dbHandle->prepare($query);

    // Then execute the query
    $statement->execute();

    // This will return the row from the database as an array
    return $statement->fetchAll(PDO::FETCH_ASSOC);
   }

Then in my Admin Controller I have:

$Admin = new Admin();
$CountUsers = $Admin->CountUsers();

To display the results I have this in my phtml:

<var><?php echo $CountUsers; ?></var>

Does anyone understand what I am doing wrong?

3
  • You're trying to output the results of a PDO statement. If it's an array, you might try echo count($CountUsers); instead Commented Dec 30, 2014 at 1:10
  • fetchAll() returns an array. You store that array into $CountUsers, so you can't echo it directly. Either access an element like echo $CountUsers['somekey'] or count($CountUsers) Commented Dec 30, 2014 at 1:11
  • 2
    Anyway, you are fetching all rows in the db just to count them, which is highly unperformant. Sql knows stuff like COUNT() Commented Dec 30, 2014 at 1:11

1 Answer 1

2

You should try to count the users in query, like so:

public function CountUsers() {
    $query = "SELECT COUNT(1) FROM Users";

    $statement = $this->_dbHandle->prepare($query);

    if($statement->execute()) {
        // This will return COUNT(1) field
        return $statement->fetch(PDO::FETCH_NUM)[0];
    }else{
        return 0; // should the query fail, return 0
    }
}

If you want to stick to counting in PHP (which you shouldn't), modify your code to do:

return count($statement->fetchAll(PDO::FETCH_ASSOC));

But you shouldn't query all of the data just to count and discard it. Using SQL's count is far more performant for several reasons:

  • You don't fetch the actual data. If your data set is big, you have to transfer megabytes or gigabytes over sockets. Which is not too good idea, even if the database is on the same machine.
  • You don't store the data in PHP. When using fetchAll, it will store all of the data into PHP array, taking up memory
  • MySQL engine might use optimisations which further speed things up (looking up the record count directly)

To display a meaningful text, or do something with this function you can:

function CheckUsers() {
    $userCount = CountUsers();
    if($userCount == 0) {
        echo 'No users found';
    }else{
        echo $userCount . ' users found';
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

You see, depending on whats returned I want to perform another SQL statement within the same function if you understand? So if 0 users returned echo "no users" however if more than 0 users were returned then perform another statement
@KovachSullivan Alright, I get it now. I have added some example code to the answer (just do your query in the else part)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.