no message
This commit is contained in:
67
Ex02.php
Normal file
67
Ex02.php
Normal file
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* [Ex02 PDO SELECT クエリと結果の返却]
|
||||
*
|
||||
* TODO:
|
||||
* 1. 新しいファイル "Ex02_query.php" を作成し、以下の処理を実装してください。
|
||||
* - PDO を使用して MySQL データベース "employee_db" に接続する。
|
||||
* DSN は "mysql:host=localhost;dbname=employee_db;charset=utf8"、ユーザは "rezouser"、パスワードは"Rezo_0000"。
|
||||
* - 関数 getEmployees() を定義し、"employees" テーブルから全レコードを取得する SELECT クエリを実行する。
|
||||
* - fetchAll() を用いて、結果セットを PDO::FETCH_ASSOC 形式で取得し、その結果を return する。
|
||||
* >> こんな感じです:
|
||||
* $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
*
|
||||
* 2. 補足説明:
|
||||
* - PDO::FETCH_ASSOC を使用すると、各行が連想配列として取得できます。
|
||||
* - この関数の返り値は、後で別のPHPファイルで require_once して表示に利用します。
|
||||
*/
|
||||
|
||||
|
||||
// Ex02.php - 表示用ファイル
|
||||
require_once __DIR__ . '/Ex02_query.php';
|
||||
|
||||
$employees = getEmployees();
|
||||
?>
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Employees List</title>
|
||||
<link href="../common/css/bootstrap.min.css" rel="stylesheet">
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div class="container">
|
||||
<h1>Employees List</h1>
|
||||
<?php if (!empty($employees)): ?>
|
||||
<table class="table table-bordered">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>ID</th>
|
||||
<th>Name</th>
|
||||
<th>従業員ID</th>
|
||||
<th>プロジェクトID</th>
|
||||
<th>Salary</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php foreach ($employees as $emp): ?>
|
||||
<tr>
|
||||
<td><?php echo htmlspecialchars($emp['id']); ?></td>
|
||||
<td><?php echo htmlspecialchars($emp['name']); ?></td>
|
||||
<td><?php echo htmlspecialchars($emp['dep_id']); ?></td>
|
||||
<td><?php echo htmlspecialchars($emp['project_id']); ?></td>
|
||||
<td><?php echo htmlspecialchars($emp['salary']); ?></td>
|
||||
</tr>
|
||||
<?php endforeach; ?>
|
||||
</tbody>
|
||||
</table>
|
||||
<?php else: ?>
|
||||
<p>No employee data found.</p>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
Reference in New Issue
Block a user