2008年9月10日 星期三

[PDO]用rowCount() 來得知是否UPDATE與INSERT成功.

看下列程式範例就知道了:

$query = "update `payment` set `status` = '2', `tx_id` = ?, `tx_method` = ?
where `payment_id` = ? and `status` = '1'";
$stmt = $this->pdo->prepare ( $query );
$stmt->bindParam ( 1, $tx_id );
$stmt->bindParam ( 2, $tx_type );
$stmt->bindParam ( 3, $payment_id );
$result = $stmt->execute ();

if($result === true) {
if($stmt->rowCount() == 1) {
$this->log_info ( " updated successful." );
} else {
$this->log_info ( " payment updated failed." );
$this->pdo->rollBack ();
return false;
}
} else {
$this->pdo->rollBack ();
$this->log_info ( " updated failed. Query Execution Failed." );
return false;
}

【下列文章您可能也有興趣】

沒有留言: