Ex.02
This commit is contained in:
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