We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

How to use rowCount or affectedRows in PhalconPHP ?

I have 2 lines of code that deletes some data from the database:

$del_cust_recipe = $this->connection->prepare("DELETE FROM custom_ingredients_to_recipe WHERE owner_id=:owner_id AND recipe_id=:recipe_id"); $res_cust_recipe = $this->connection->executePrepared($del_cust_recipe,array('owner_id'=>$this->user_id,'recipe_id'=>$recipe_id), array('owner_id' => \Phalcon\Db\Column::TYPE_INTEGER,'recipe_id' => \Phalcon\Db\Column::TYPE_INTEGER));

It works, but I would like to know how many rows are being affected. I have tried testing for $res_cust_recipe->rowCount() or $res_cust_recipe->affectedRows() but both lead to a method not known error.

Just testing with if($res_cust_recipe) {} wouldn't cut it, as this also returns true when the affected rows are 0. Which it should: the query was succesful, so...

However I need to vary my code based on the amount of rows affected.

Are these PDO methods (rowCount and affectedRows) just not available in PhalconPHP ?

Thanks for your help, Alex

edited Nov '14

Normally they should be available because the executePrepared method returns a \PDOStatement object of PHP.

I just tried it in a test environment and it works. I got the correct number of deleted records with rowCount() method.

BTW: Can you try to format you code with the code syntax? It's a bit hard to read and variables are different ($delcustrecipe => $del_custrecipe).


You are right, not sure why I couldn't get it to work earlier... Thanks!


Something else has popped up though, which I think must be some sort of bug, not sure whether it's Phalcon or PDO (couldn't find it when Googling). Or it's just my bad. I believe it is a separate issue though, have posted it therefore here: