How to check if UPDATE was successful

How to check if UPDATE statement was successful??? I can't find it http://docs.phalconphp.com/en/latest/reference/phql.html

$success = $query->execute();
if ($success) {
    $this->flash->success("ok");
}

is always returning true

$result = $query->execute();

$result->success()

is always returning "1"



5.8k
edited Mar '14

that suppose to be a successful update,

check in your database that records are correctly updated, or query again the record and try to create a fail by providing wrong values for the fields to see what is the $success in that case

edited Mar '14

maybe this will work:

$success = $query->execute();
if ($success != false) {
    $this->flash->success("ok");
}

Try this:

$phql = "UPDATE Cars SET color='red' WHERE id=1";
$result = $this->modelsManager->executeQuery($pqhl);
if($result->success() == true)
{
    //do something
}


15.5k
edited Mar '14

The problem is that $result->success() is always == 1 and $success is always != false even when UPDATE is not successful



83.0k

When does the update is not successful for you?



15.5k
edited Mar '14
$query = $this->modelsManager->createQuery(
                "UPDATE Users SET email_code = 0, password = :password: WHERE email = :email: AND email_code = :email_code:");

$result = $query->execute(array(
    'pass' => $password,
    'email' => $email,
    'email_code' => $email_code
));

$email and $email_code are (parameters) from url.

$result->success() is always ==1 even when UPDATE really take place and when parameters are wrong.



83.0k

Why it is not successful? It's successful if the validators defined on the model Users do not reject the values used as parameters.



15.5k

So query which is not changing (updating) anything is successful too????(wrong parameters)



15.5k
edited Mar '14
public function someAction($email)
{
        $query = $this->modelsManager->createQuery(
            "UPDATE Users SET name = 'NameAfterUpdate' WHERE email = :email:");

        $result = $query->execute(array(
            'email' => $email
        ));

        if ($result->success()==true) {
               $this->flash->success("ok");
         }
         else {
               $this->flash->error("error");
         }
 }

$result->success() always return true. I've got Phalcon\Mvc\Model\Validator\Email validation in model Users and even when $email = 'aaaaaaaaaa' $result->success()==true

edited Mar '14

If you can get DB connection try this after running execute()

$connection->affectedRows();

You can find more in documentation: http://docs.phalconphp.com/en/latest/api/Phalcon%5Db%5Adapter%5Pdo.html



83.0k
edited Mar '14

So, according to your expected behavior this must work:

$pdo = new PDO ("mysql:host=localhost;dbname=test","root","");

//Delete all records
$pdo->exec("DELETE FROM users");

//SQL is valid but no records to update
if (!$pdo->exec("UPDATE users SET name = 'some name'")) {
    echo "no records to update or an error has occurred";
}
edited May '14

I am looking for an answer to a similar issue. Is there no way to get the affected rows using ORM/PHQL on an UPDATE/DELETE? Do I have to resort to Raw SQL to get the affected rows?

I am using query method for UDPATE/DELETE using Raw SQL. Phalcon docs suggests using execute method for UPDATE/DELETE. The following works.

$result = $connection->query($updateSql, $params);

if ($result->numRows()) {
    //do something on success
}

Resulting object belongs to \Phalcon\Db\Result\Pdo.



3.1k

I second the problems described above. It would be great if the PDO statements themselves would indicate whether a query is succesful or not, by means of a BOOLEAN.

E.g. this is my code:

$delcustrecipe = $this->connection->delete('customrecipes','ownerid=:ownerid AND recipeid=:recipeid',array('ownerid'=>$this->userid,'recipeid'=>$recipe_id));

$delcustrecipe always returns true here, even though no rows were deleted....

I have similar issue with update and delete query