Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

How to use rowCount or affectedRows in PhalconPHP ?

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

$delcustrecipe = $this->connection->prepare("DELETE FROM customingredientstorecipe WHERE ownerid=:ownerid AND recipeid=:recipeid"); $rescustrecipe = $this->connection->executePrepared($delcustrecipe,array('ownerid'=>$this->userid,'recipeid'=>$recipeid), array('ownerid' => \Phalcon\Db\Column::TYPEINTEGER,'recipeid' => \Phalcon\Db\Column::TYPE_INTEGER));

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

Just testing with if($rescustrecipe) {} 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



2.3k
Accepted
answer
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).



3.1k

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



3.1k

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:

http://forum.phalconphp.com/discussion/4107/potential-phalcon-bug-or-is-pdo-my-code-to-blame