回答です

This commit is contained in:
a-kizaki
2026-10-28 16:53:31 +09:00
parent 06c1988850
commit fcaf9d4b5f
28 changed files with 10572 additions and 0 deletions

View File

@@ -0,0 +1,62 @@
<?php
// Ex07.php
$dsn = "mysql:host=localhost;dbname=employee_db;charset=utf8";
$username = "rezouser";
$password = "Rezo_0000"; // 各環境に合わせて設定してください
try {
if (!isset($_GET['id'])) {
throw new Exception("画像IDが指定されていません。");
}
$imageId = (int)$_GET['id'];
$pdo = new PDO($dsn, $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT filename, mime, image FROM images WHERE id = :id";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':id', $imageId, PDO::PARAM_INT);
$stmt->execute();
// bindColumn() を使用して各カラムの値を変数にバインド
$stmt->bindColumn('filename', $fetchedFilename);
$stmt->bindColumn('mime', $fetchedMime);
$stmt->bindColumn('image', $fetchedImage, PDO::PARAM_LOB);
if (!$stmt->fetch(PDO::FETCH_BOUND)) {
throw new Exception("画像情報が取得できませんでした。");
}
if (is_resource($fetchedImage)) {
$fetchedImage = stream_get_contents($fetchedImage);
}
$base64Image = base64_encode($fetchedImage);
} catch (Exception $e) {
echo "エラー: " . $e->getMessage();
exit;
} finally {
if (isset($pdo)) {
$pdo = null;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登録された画像の表示</title>
<link href="common/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<h1><?php echo htmlspecialchars($fetchedFilename); ?></h1>
<img src="data:<?php echo htmlspecialchars($fetchedMime); ?>;base64,<?php echo $base64Image; ?>"
alt="<?php echo htmlspecialchars($fetchedFilename); ?>" class="img-responsive" />
</div>
</body>
</html>