27

How can I test to see if the following query executed successfully?

$STH = $this->_db->prepare("UPDATE UserCreds SET
    VerificationString=:newVerificationString, ExpiryDate=:expiryDate 
    WHERE UserID = :userID;");
$STH->execute($params);

I know I can use lastInsertId() when I'm adding new rows, but what about UPDATEs and SELECTs?

Henders
  • 1,195
  • 1
  • 21
  • 27
Chuck Le Butt
  • 47,570
  • 62
  • 203
  • 289
  • @Pekka Nope, I can get error messages just fine. – Chuck Le Butt Mar 01 '12 at 17:47
  • 4
    http://de.php.net/manual/en/pdostatement.execute.php says: *Returns TRUE on success or FALSE on failure.* - So bind it to a variable, e.g. `$success = $STH->execute($params);` and check that variable against `true` or `false`. – Quasdunk Mar 01 '12 at 17:47

4 Answers4

51

The execute returns true on success and false on failure.

From Docs:

Returns TRUE on success or FALSE on failure.


So you can make sure query ran successfully like:

if ($STH->execute($params))
{
  // success
}
else
{
  // failure
}
Sarfraz
  • 377,238
  • 77
  • 533
  • 578
  • So something like `if($STH->execute($params)) { echo "Success!"; } else { echo "Failure!"; } `? – Chuck Le Butt Mar 01 '12 at 17:48
  • 1
    @DjangoReinhardt: Yes you are right :) You can also store result of `execute` in a variable if you need to use that later. – Sarfraz Mar 01 '12 at 17:49
  • 7
    Works great for just checking (technical) success. Use `rowCount()` to check whether anything has changed in an `UPDATE` (the `mysql` driver will also tell you how much rows are in a `SELECT` with it, but this shouldn't be relied upon). – Wrikken Mar 01 '12 at 17:59
  • 1
    If you test for type it doesn't work. E.G. if( $STH->execute($params) === true ) Will fail, every time. At least when the execute if for creating a new mysql user. Returns int(0) on success, and null on failure. Why is that? – Jeff Vdovjak Feb 13 '15 at 20:35
  • 1
    how do you get the error message to help figure out why it failed? – nomaam Jan 04 '21 at 01:26
18

execute() returns true/false based on success/failure of the query:

$status = $STH->execute($params);

if ($status) {
   echo 'It worked!';
} else {
   echo 'It failed!';
}

One note: a select query which returns no rows is NOT a failure. It's a perfectly valid result that just happens to have NO results.

Marc B
  • 356,200
  • 43
  • 426
  • 500
8

simply i can count the number of row effected:

$stmt->execute();
$count = $stmt->rowCount();

if($count =='0'){
    echo "Failed !";
}
else{
    echo "Success !";
}
Bashir Noori
  • 639
  • 7
  • 12
-3

This is best way to verify Doctrine query result return success or failed result and same way as per above suggest to check weather query returns Success on True and Failed on false.

    public static function removeStudent($teacher_id){
        $q = Doctrine_Query::create()
            ->delete('Student s')
            ->where('s.teacher_id = ?');

        $result = $q->execute(array($teacher_id));

        if($result)
        {
         echo "success";
        }else{
         echo "failed";
        }
    }
Nikhil Thombare
  • 1,058
  • 2
  • 11
  • 26