オブジェクトの配列を含むオブジェクトを作成する関数があります。ただし、SQL 構文にエラーがある場合、エラーは表示されません。$queryGetImages の book_images を book_images_blabla に変更すると、準備が失敗し、else が呼び出されます。そこでエラーがスローされますが、最初に $stmt オブジェクトを閉じる必要があります。でも。これを閉じると、エラーはもう利用できません。そんなことがあるものか?エラーは $stmt ではなく $stmtImages でスローされるためですか?
public function getBook($number){
$query = "select title from book where book_id = ?";
if ($stmt = $this->database->getConnection()->prepare($query)) {
$stmt->bind_param("i",$number);
$stmt->execute();
$stmt->bind_result($title);
$stmt->store_result();
if(($stmt->num_rows) > 0){
$stmt->fetch();
$book = new Book($title);
//get images
$queryGetImages = "select imageUrl from book_images where book_id = ?";
if ($stmtImages = $this->database->getConnection()->prepare($queryGetImages)) {
$stmtImages->bind_param('i',$number);
$stmtImages->execute();
$stmtImages->bind_result($imageUrl);
$stmtImages->store_result();
if(($stmtImages->num_rows) > 0){
$images = array();
while($stmtImages->fetch()){
$image = new Image($imageUrl);
array_push($images,$image);
}
$book->images = $images;
}
$stmtImages->close();
}else{
echo $this->database->getConnection()->error; //this works
$stmt->close();
echo $this->database->getConnection()->error; //this doesn't
throw new Exception('Error: ' . $this->database->getConnection()->error;);
}
}
$stmt->close();
return $book;
}else{
throw new Exception('Error: ' . $this->database->getConnection()->error;);
}
}