Page 1 of 1

DAO pattern help

Posted: Fri Jun 19, 2009 1:31 pm
by JohnBoy2
Hello all. My apologies if this is not the best place to post this, but it seemed so after reviewing the categories.

I have a DAO class that I am trying to make orthogonal in supporting transactions. A code snippet paints a thousand words:

Code: Select all

 
  public static function getPersonTestById($value) {
    $retVal = null;
    try {
      $conn = self::getConnection();
      $retVal = self::getPersonTestByIdTrans($value, $conn);
      $conn->close();
    }
    catch (Exception $e) {
      echo "PersonTestDAO::getPersonTestById():  Exception thrown.  Details:  " . $e;
    }
    return $retVal;
  }
 
  public static function getPersonTestByIdTrans($value, $conn) {
    $theSQL = "select * from PersonTests where id = ?;";
 
    $stmt = $conn->prepare($theSQL);
 
    $stmt->bind_param("i", $value);
    $stmt->execute();
 
    if ($conn->error) throw new Exception("PersonTestDAO::getPersonTestByIdTrans():  Exception thrown.  $conn->error: " . $conn->error);
 
    $retVal = self::bindPersonTest($stmt);
 
    $stmt->close();
 
    return $retVal;
  }
 
I'm trying to understand Exceptions, try-catch blocks, and mysqli error reporting all at once, and I'm just not sure I have this right in this case.

Should the code in the getPersonTestByIdTrans() function be wrapped in a try-catch? Or is the $mysqli_error variable the only way that mysqli communicates problems?

Many thanks to all who reply.

John