2012-02-24 2 views
0

PDO 데이터베이스 클래스가 있습니다. PDO에 약간 익숙하므로 오류가 발생하면 디버깅 할 수있는 방법을 모릅니다. 나는 단지 오류를 잡아서 요청으로 돌려 보내는 가장 좋은 방법을 알고 싶다.PDO 데이터베이스 클래스에서 오류가 발생했습니다.

class database { 

private $dbh; 
private $stmt; 

public function __construct($user, $pass, $dbname) { 
    $this->dbh = new PDO(
     "mysql:host=localhost;dbname=$dbname", 
     $user, 
     $pass, 
     array(PDO::ATTR_PERSISTENT => true) 
    ); 

} 

public function query($query) { 
    $this->stmt = $this->dbh->prepare($query); 
    return $this; 
} 

public function bind($pos, $value, $type = null) { 

if(is_null($type)) { 
     switch(true) { 
      case is_int($value): 
       $type = PDO::PARAM_INT; 
       break; 
      case is_bool($value): 
       $type = PDO::PARAM_BOOL; 
       break; 
      case is_null($value): 
       $type = PDO::PARAM_NULL; 
       break; 
      default: 
       $type = PDO::PARAM_STR; 
     } 
    } 

    $this->stmt->bindValue($pos, $value, $type); 
    return $this; 
} 

public function execute() { 
    return $this->stmt->execute(); 
} 

public function resultset() { 
    $this->execute(); 
    return $this->stmt->fetchAll(); 
} 

public function single() { 
    $this->execute(); 
    return $this->stmt->fetch(); 
    } 
} 

감사

+2

관련 항목 : http://stackoverflow.com/questions/3726505/how-to-squeeze-error-message-out-of-pdo –

+0

pekka가 게시 한 링크를 통해 이동하십시오, 그는 대답이 –

답변

1

당신은 당신의 "클라이언트 코드"에서 오류를 잡을 수 있습니다 : 여기 내 데이터베이스 클래스입니다. "실행"또는 "쿼리"메소드를 포함하는 모든 호출에서 try/catch 블록을 사용하십시오. 그런 다음 오류가 발생할 때마다 원하는대로 응용 프로그램을 시작할 수 있습니다.

+0

있습니다. 감사합니다, 둘 다 그리고 Pekka 도왔습니다. –

관련 문제