在try / cach内部或外部准备好的声明?(Prepared statement inside or outside a try/cach?)

我怀疑的是准备好的语句是在try / catch块之内还是之外。

(这是我的User类的示例方法)

我应该这样做吗?

public function getEmail( $id_user ) { $this->_sql = 'SELECT Email FROM '.TBL_USERS.' WHERE IdUser = :id_user'; $stmt = $this->_db->prepare($this->_sql); try { $stmt->bindParam(':id_user', $id_user, PDO::PARAM_INT); $stmt->execute(); $row = $stmt->fetchObject(); if (is_object($row)) { return $row->Email; } return NULL; } catch (PDOException $e) { throw $e; } }

或这个?

public function getEmail( $id_user ) { $this->_sql = 'SELECT Email FROM '.TBL_USERS.' WHERE IdUser = :id_user'; try { $stmt = $this->_db->prepare($this->_sql); $stmt->bindParam(':id_user', $id_user, PDO::PARAM_INT); $stmt->execute(); $row = $stmt->fetchObject(); if (is_object($row)) { return $row->Email; } return NULL; } catch (PDOException $e) { throw $e; } }

My doubt is whether the prepared statement should be inside or outside a try/catch block.

(this is an example method from my User class)

Should I do this?

public function getEmail( $id_user ) { $this->_sql = 'SELECT Email FROM '.TBL_USERS.' WHERE IdUser = :id_user'; $stmt = $this->_db->prepare($this->_sql); try { $stmt->bindParam(':id_user', $id_user, PDO::PARAM_INT); $stmt->execute(); $row = $stmt->fetchObject(); if (is_object($row)) { return $row->Email; } return NULL; } catch (PDOException $e) { throw $e; } }

or this?

public function getEmail( $id_user ) { $this->_sql = 'SELECT Email FROM '.TBL_USERS.' WHERE IdUser = :id_user'; try { $stmt = $this->_db->prepare($this->_sql); $stmt->bindParam(':id_user', $id_user, PDO::PARAM_INT); $stmt->execute(); $row = $stmt->fetchObject(); if (is_object($row)) { return $row->Email; } return NULL; } catch (PDOException $e) { throw $e; } }

最满意答案

prepare()方法可能会抛出PDOException,因此您应该在try块内包含要准备的调用。 然而,在这两个例子中,你只是重新抛出异常。 除非你实际上要处理catch块内的异常,否则效果将是相同的。

The prepare() method can potentially throw a PDOException so you should include the call to prepare inside of the try block. However in both of the examples you're just re-throwing the exception. Unless you're actually going to handle the exception inside of the catch block the effect will be the same.

在try / cach内部或外部准备好的声明?(Prepared statement inside or outside a try/cach?)

我怀疑的是准备好的语句是在try / catch块之内还是之外。

(这是我的User类的示例方法)

我应该这样做吗?

public function getEmail( $id_user ) { $this->_sql = 'SELECT Email FROM '.TBL_USERS.' WHERE IdUser = :id_user'; $stmt = $this->_db->prepare($this->_sql); try { $stmt->bindParam(':id_user', $id_user, PDO::PARAM_INT); $stmt->execute(); $row = $stmt->fetchObject(); if (is_object($row)) { return $row->Email; } return NULL; } catch (PDOException $e) { throw $e; } }

或这个?

public function getEmail( $id_user ) { $this->_sql = 'SELECT Email FROM '.TBL_USERS.' WHERE IdUser = :id_user'; try { $stmt = $this->_db->prepare($this->_sql); $stmt->bindParam(':id_user', $id_user, PDO::PARAM_INT); $stmt->execute(); $row = $stmt->fetchObject(); if (is_object($row)) { return $row->Email; } return NULL; } catch (PDOException $e) { throw $e; } }

My doubt is whether the prepared statement should be inside or outside a try/catch block.

(this is an example method from my User class)

Should I do this?

public function getEmail( $id_user ) { $this->_sql = 'SELECT Email FROM '.TBL_USERS.' WHERE IdUser = :id_user'; $stmt = $this->_db->prepare($this->_sql); try { $stmt->bindParam(':id_user', $id_user, PDO::PARAM_INT); $stmt->execute(); $row = $stmt->fetchObject(); if (is_object($row)) { return $row->Email; } return NULL; } catch (PDOException $e) { throw $e; } }

or this?

public function getEmail( $id_user ) { $this->_sql = 'SELECT Email FROM '.TBL_USERS.' WHERE IdUser = :id_user'; try { $stmt = $this->_db->prepare($this->_sql); $stmt->bindParam(':id_user', $id_user, PDO::PARAM_INT); $stmt->execute(); $row = $stmt->fetchObject(); if (is_object($row)) { return $row->Email; } return NULL; } catch (PDOException $e) { throw $e; } }

最满意答案

prepare()方法可能会抛出PDOException,因此您应该在try块内包含要准备的调用。 然而,在这两个例子中,你只是重新抛出异常。 除非你实际上要处理catch块内的异常,否则效果将是相同的。

The prepare() method can potentially throw a PDOException so you should include the call to prepare inside of the try block. However in both of the examples you're just re-throwing the exception. Unless you're actually going to handle the exception inside of the catch block the effect will be the same.