Compare commits

...

2 Commits

Author SHA1 Message Date
3e584d4044 Ex.02 2026-10-28 16:54:17 +09:00
6a0949ca3f Ex.01 完了 2026-10-28 15:24:50 +09:00
3 changed files with 55 additions and 0 deletions

View File

@@ -12,3 +12,20 @@
* *
* ※この問題を解く前に、setup.sql を実行してデータベースをセットアップしてください。 * ※この問題を解く前に、setup.sql を実行してデータベースをセットアップしてください。
*/ */
// DSN は "mysql:host=localhost;dbname=employee_db;charset=utf8"
$dsn = 'mysql:host=localhost;dbname=employee_db;charset=utf8';
// データベース接続に必要なユーザ名とパスワード
$user = "rezouser";
$password = 'Rezo_0000';
try{
// 橋をかける
$dbh = new PDO($dsn, $user, $password);
echo '接続しました';
} catch (Throwable $th){
echo '失敗しました: ' . $th->getMessage();
} finally {
$dbh = null;
}

View File

@@ -22,6 +22,7 @@
require_once __DIR__ . '/Ex02_query.php'; require_once __DIR__ . '/Ex02_query.php';
$employees = getEmployees(); $employees = getEmployees();
?> ?>
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>

37
Ex02_query.php Normal file
View File

@@ -0,0 +1,37 @@
<?php
// - 関数 getEmployees() を定義し、
function getEmployees(){
// どのデータベースに接続するか指定
$dsn = 'mysql:host=localhost;dbname=employee_db;charset=utf8';
// データベース接続に必要なユーザ名とパスワード
$user = 'rezouser';
$password = 'Rezo_0000';
}
try{
// 橋をかける
$dbh = new PDO($dsn, $user, $password);
// SQL文を作成
// INSERT INTO employees
$stt = $dbh->prepare('INSERT INTO employees
(id,name,dep_id,project_id,salary)
VALUES (:id, :name, :dep_id, :project_id, :salary)');
$stt->bindValue(':id',$_POST['id']);
$stt->bindValue(':name',$_POST['name']);
$stt->bindValue(':dep_id',$_POST['dep_id']);
$stt->bindValue(':project_id',$_POST['project_id']);
$stt->bindValue(':salary',$_POST['salary']);
// "employees" テーブルから全レコードを取得する SELECT クエリを実行する。
$sql = "SELECT * FROM employees";
// - fetchAll() を用いて、結果セットを PDO::FETCH_ASSOC 形式で取得し、その結果を return する。
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
// SQLを実行
$stmt->execute();
return $results;
}