2011-11-30 10 views
0

を取った場合、私はちょうどこのコードを実行しようとしていた:PDOは、エラーを受け取り、

public function create_account($username, $password, $email) { 
    $password = sha1($password); 
    $activation_key = md5($username . $email); 

    $this->STH = $this->DBH->prepare("INSERT INTO users(username, password, email, activation_key, created) VALUES(:username, :password, :email, :activation_key, :created)"); 
    $this->STH->bindParam(':username', $username); 
    $this->STH->bindParam(':password', $password); 
    $this->STH->bindParam(':email', $email); 
    $this->STH->bindParam(':activation_key', $activation_key); 
    $this->STH->bindParam(':created', time()); 
    $this->STH->execute(); 

    if ($this->DBH->lastInsertId()) { 
     //$this->send_mail($username, $email); 
     return true; 
    } else { 
     //Log 
    } 
} 

は、今では確かに、取り組んでいます。しかし、ちょうど1分前に、私はデータが挿入されていないことを導いたSQL文でa)を逃しました。私はtry {}キャッチブロックを試しました - しかし、私は何のエラーも受けていませんでした - それはそうだと思いますか? PDOエラー(受信した場合)を適切に記録するには、このコードを修正する方法を教えてください。

+2

おそらく関連しています:[PDOからエラーメッセージを取り出す方法](http://stackoverflow.com/q/3726) 505) –

答えて

1

​​はboolを返します。基本的にはこのようにそれを使用することができます:

if ($this -> STH -> execute() === false) { 
    print_r($this -> STH -> errorInfo()); 
} 
+2

また、try/catchブロックを追加し、複数の挿入がある場合は、例外がスローされた場合にトランザクションとロールバックを試してみるとよいでしょう。 – Homer6

1

あなたが使用してエラーをキャッチしようとしたことがあり:

try { 
    $this->STH->execute(); 
} catch (PDOException $pdostmtex) { 
    echo('Exception encountered: '.$pdostmtex->getCode().' -> '.$pdostmtex->getMessage()); 
    exit; 
} 

そして、あなたのエラー報告が上であることを確認してください。

http://php.net/manual/en/function.error-reporting.phpから撮影し、専用として使用します参照:

// Report all errors except E_NOTICE 
// This is the default value set in php.ini 
error_reporting(E_ALL^E_NOTICE); 

// Report all PHP errors (see changelog) 
error_reporting(E_ALL); 
関連する問題