Compare commits
3 Commits
master
...
1c2fc7d118
| Author | SHA1 | Date | |
|---|---|---|---|
| 1c2fc7d118 | |||
| 3e584d4044 | |||
| 6a0949ca3f |
17
Ex01.php
17
Ex01.php
@@ -12,3 +12,20 @@
|
||||
*
|
||||
* ※この問題を解く前に、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;
|
||||
}
|
||||
1
Ex02.php
1
Ex02.php
@@ -22,6 +22,7 @@
|
||||
require_once __DIR__ . '/Ex02_query.php';
|
||||
|
||||
$employees = getEmployees();
|
||||
|
||||
?>
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
37
Ex02_query.php
Normal file
37
Ex02_query.php
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user