2

I see other tutorials on counting rows, but none on counting specific rows $row[scope] for specific results. How can this be done?

function counting(){
$query = "SELECT    * 
          FROM      table";

$result->fetchAll($sql, $params);   
$num_rows = count($result);

$counter = 0;

    //while($row = mysql_fetch_array($result)){ //old code
    foreach($result as $row){
        if($row[scope] == "all"){
            $counter++;
        }
    }
    $return = ($counter > 0)?TRUE:FALSE;
}

protected function fetchAll($sql, $params)
{
    $stmt = $this->dbh->prepare($sql);
    $stmt->execute($params);
    return $stmt->fetchALL(DatabaseAdapter::FETCH_ASSOC);
}
6
  • 2
    Hey, why don't you use the SQL language to do that for you? It's quite the right tool for the job in this case. (e.g) SELECT * FROM table where scope = 'all'; -- At that point you won't need PHP to filter it Commented Sep 8, 2016 at 20:34
  • This is just a piece of a long formula of about 5 different rows in the function (even though I made it simple), so I wanted to know the basics. Commented Sep 8, 2016 at 20:35
  • 3
    The basics are to let sql do it 100x quicker. Like select count(*) as theCount from myTable where scope='all' Commented Sep 8, 2016 at 20:36
  • Is there no way to count with foreach? Commented Sep 8, 2016 at 20:38
  • 3
    Sure, if you want to drag your system to a halt and not learn sql :p Commented Sep 8, 2016 at 20:38

1 Answer 1

2

You need two functions for that.

Instead of fetchAll() you need just a general purpose function to run a query that returns PDOStatement which is essential.

protected function sql($sql, $params)
{
    $stmt = $this->dbh->prepare($sql);
    $stmt->execute($params);
    return $stmt;
}

in most cases it will work exactly the same way as your fetchAll() function without any changes in the code, but in case you explicitly need to get an array from it, you can always attach a fetchAll() to its call:

$result = $this->sql($sql, $params)->fetchAll();

And the second function you need is for the actual count:

function counting($value)
{
    $query = "SELECT count(*) FROM table WHERE scope = ?";
    return $this->sql($sql, [$value])->fetchColumn();
}

using such a function you can count (or detect the existence) for any scope.

Note that thanks to returned PDOStatement we are able to get another type of result - a scalar value instead of array.

upgrading While loops can be a pain sometimes....

You see, when SQL and PDO are properly used, you scarcely need a while loop at all

Sign up to request clarification or add additional context in comments.

3 Comments

So you have to hit the database twice. This helped clarify this. Please fix fethColumn for other users when they see this. :)
sorry, why twice?
Sorry, I was using this logic for a function of counting within another function that I didn't add to the question itself. You are correct with your logic!

Your Answer

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