ここでは PDO - Part2 の学習のまとめとして演習課題に取り組みます。
サンプルデータの準備
ここでは簡単なEラーニングシステムの開発を想定して、以下の2つのテーブルを作成して学習を進めます。

categoriesテーブルcoursesテーブル
以前に利用していたテーブルと変わりありません。レコードを再登録して学習しやすいように準備します。
categories テーブル
categories テーブルには以下の3件のレコードを作成します。
| id | title |
|---|---|
| 1 | Programming |
| 2 | Design |
| 3 | Marketing |
categories テーブルの作成に必要なSQLは以下のとおりです。
drop table if exists categories;
create table categories(
id integer primary key,
title varchar(100)
);
insert into categories (id, title) values (1, 'Programming');
insert into categories (id, title) values (2, 'Design');
insert into categories (id, title) values (3, 'Marketing');
courses テーブル
courses テーブルはテーブルのみ作成します。テーブル内のレコードは後の課題で作成します。
courses テーブルの作成に必要なSQLは以下のとおりです。
drop table if exists courses;
create table courses(
id integer primary key,
title varchar(100),
learning_time integer,
category_id integer
);
1 PreparedStatementによるレコードの取得
PDO インスタンスを生成し、以下の select 文を実行して期待値のとおり出力するプログラム( pdo_tr5.php )を作成します。
select * from categories where id = ?
pdo_tr5.php
<?php
$id = 3;
$sql = "select * from categories where id = ?";
try {
$dsn = "mysql:host=localhost;dbname=eldb;charset=utf8mb4";
$username = "root";
$password = "admin";
$pdo = new PDO($dsn, $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
// TODO
} catch (PDOException $e) {
echo $e->getMessage() . PHP_EOL;
}
Terminal
$ php pdo_tr5.php
Marketing
2 PreparedStatementによるレコードの登録
PDO インスタンスを生成し、以下の insert 文を実行して期待値のとおり出力するプログラム( pdo_tr6.php )を作成します。ただし実行する複数の SQL は1つのトランザクションで処理するものとします。
insert into courses (id, title, learning_time, category_id)
values (:id, :title, :learning_time, :category_id)
pdo_tr6.php
<?php
$courses = [
["id" => 1, "title" => "PHP Basic", "learning_time" => 0, "category_id" => 1],
["id" => 2, "title" => "PHP Database", "learning_time" => 0, "category_id" => 1],
["id" => 3, "title" => "Python Basic", "learning_time" => 0, "category_id" => 1],
["id" => 4, "title" => "Web Design", "learning_time" => 0, "category_id" => 2],
["id" => 5, "title" => "Japan's History", "learning_time" => 0, "category_id" => null],
];
$sql = "insert into courses (id, title, learning_time, category_id)
values (:id, :title, :learning_time, :category_id)";
try {
$dsn = "mysql:host=localhost;dbname=eldb;charset=utf8mb4";
$username = "root";
$password = "admin";
$pdo = new PDO($dsn, $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
// TODO
} catch (PDOException $e) {
echo $e->getMessage() . PHP_EOL;
}
登録するレコードの
learning_timeはすべて0としています。
Terminal
$ php pdo_tr6.php
Insert count: 5
画面に更新件数(登録件数)を表示します。
実行結果:coursesテーブル
| id | title | learning_time | category_id |
|---|---|---|---|
| 1 | PHP Basic | 0 | 1 |
| 2 | PHP Database | 0 | 1 |
| 3 | Python Basic | 0 | 1 |
| 4 | Web Design | 0 | 2 |
| 5 | Japan's History | 0 |
3 PreparedStatementによるレコードの更新
PDO インスタンスを生成し、以下の update 文を実行して期待値のとおり出力するプログラム( pdo_tr7.php )を作成します。
update courses set learning_time = :learning_time where id = :id
pdo_tr7.php
<?php
$courses = [
["id" => 1, "learning_time" => 30],
["id" => 2, "learning_time" => 50],
["id" => 3, "learning_time" => 40],
["id" => 4, "learning_time" => 50],
["id" => 5, "learning_time" => 100]
];
$sql = "update courses set learning_time = :learning_time where id = :id";
// TODO
Terminal
$ php pdo_tr7.php
Update count: 5
画面に更新件数を表示します。
実行結果:coursesテーブル
| id | title | learning_time | category_id |
|---|---|---|---|
| 1 | PHP Basic | 30 | 1 |
| 2 | PHP Database | 50 | 1 |
| 3 | Python Basic | 40 | 1 |
| 4 | Web Design | 50 | 2 |
| 5 | Japan's History | 100 |