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

Potential Phalcon bug or is PDO / my code to blame?

Hello all,

I am using rowCount() to test whether a certain query is succesful. If I run this code:

$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));

$affected = $res_cust_recipe->rowCount();
echo $affected;
exit;

$affected returns 1, indicating 1 row has been deleted. Which is correct, checking from the database. The query has executed was: DELETE FROM custom_recipes WHERE owner_id='28' AND recipe_id='33'

However, when I continue the code, to test in case 0 rows were affected (meaning nothing was deleted):

$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));

$affected = $res_cust_recipe->rowCount();
echo $affected;     

if ($affected == 0)
        {
            echo "erroneous result"
            exit;
            }               

echo $affected returns 0 (and thus erroneous result echoed), even when a DELETE query has been executed (I made sure the record was there in the table again)

This is very puzzling to me. The code is essentially the same. If I exited the code direclty, $affected is 1 (correct behaviour)....if I continue with the code and exited a few lines later, $affected is 0 (incorrect behaviour) ??

I double-checked whether an acutal row was deleted and I confirm that to be the case. So rowCount should still return 1. Am I overlooking something?

Thanks for your help! Alex



5.7k

Good tip have done that. Thanks. Alex

@aligg73 Perhaps try placing "php" after the first triple tick turn on PHP syntax highlighting.



5.7k

Bump!

Anyone's got any clue to what's causing this? Thanks for looking into it.

Alex