Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
PHPとDBの常識的な話
Search
らいあ
February 19, 2017
Programming
0
140
PHPとDBの常識的な話
らいあ
February 19, 2017
Tweet
Share
More Decks by らいあ
See All by らいあ
GoのDBライブラリについて調べてみた
ryer
0
590
shimanego3-lt.pdf
ryer
0
410
PageSpeed基礎知識
ryer
0
61
PHPひとめぐり
ryer
0
38
関西ソーシャルゲーム勉強会のご紹介
ryer
0
51
Other Decks in Programming
See All in Programming
そのAPI、誰のため? Androidライブラリ設計における利用者目線の実践テクニック
mkeeda
2
280
機能追加とリーダー業務の類似性
rinchoku
2
1.3k
Compose Multiplatform × AI で作る、次世代アプリ開発支援ツールの設計と実装
thagikura
0
140
Performance for Conversion! 分散トレーシングでボトルネックを 特定せよ
inetand
0
140
Vue・React マルチプロダクト開発を支える Vite
andpad
0
110
モバイルアプリからWebへの横展開を加速した話_Claude_Code_実践術.pdf
kazuyasakamoto
0
320
テストコードはもう書かない:JetBrains AI Assistantに委ねる非同期処理のテスト自動設計・生成
makun
0
250
パッケージ設計の黒魔術/Kyoto.go#63
lufia
3
430
Rancher と Terraform
fufuhu
2
240
CJK and Unicode From a PHP Committer
youkidearitai
PRO
0
110
Introducing ReActionView: A new ActionView-compatible ERB Engine @ Rails World 2025, Amsterdam
marcoroth
0
670
Kiroの仕様駆動開発から見えてきたAIコーディングとの正しい付き合い方
clshinji
1
210
Featured
See All Featured
The Pragmatic Product Professional
lauravandoore
36
6.9k
Imperfection Machines: The Place of Print at Facebook
scottboms
268
13k
Balancing Empowerment & Direction
lara
3
620
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
51
5.6k
KATA
mclloyd
32
14k
Fireside Chat
paigeccino
39
3.6k
Building an army of robots
kneath
306
46k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
53k
Build The Right Thing And Hit Your Dates
maggiecrowley
37
2.9k
Why Our Code Smells
bkeepers
PRO
339
57k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3k
Art, The Web, and Tiny UX
lynnandtonic
303
21k
Transcript
None
None
None
None
None
None
None
None
None
# yum install php-pdo
$DSN = ‘mysql:host=10.0.0.1;dbname=userdb’; $OPT = [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]; $pdo =
new PDO($DSN, $USER, $PASSWD, $OPT);
$DSN = ‘mysql:host=10.0.0.1;dbname=userdb’;
$OPT = [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION];
$cnt = $pdo->exec(“INSERT INTO prefs (id, pref) VALUES (1, ’沖縄県')");
$stmt = $pdo->query("SELECT * FROM prefs"); $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); var_dump($rows);
$cnt = $pdo->exec(更新SQL) $stmt = $pdo->query(選択SQL)
$rows = $stmt->fetchAll();
$pdo->beginTransaction(); try { ... $pdo->commit(); } catch (Exception $ex) {
... $pdo->rollBack(); }
簡単 でしょ?
None
None
None
None
$DSN = “mysql:dbname=db1;charset=sjis”; $DSN = “pgsql:dbname=db1; options=‘--client_encoding=sjis’”;
None
忘れずに
None
$sql = “SELECT * FROM prefs WHERE id =:id”; $stmt
= $pdo->prepare($sql); $stmt->execute([‘id’ => 123]); $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); var_dump($rows);
$stmt = $pdo->prepare(プレースホルダ付SQL文) $stmt->execute(パラメータの配列)
None
None
None
$OPT = [PDO::ATTR_EMULATE_PREPARES => false];
常識?
None
None
None
// すべての記事をSELECT $sql = “SELECT * FROM kiji”; $stmt =
$pdo->query($sql); $rows = $stmt->fetchAll(); foreach ($rows as $kiji) { // 記事の本文を更新する関数 update($kiji[’id’], $kiji[‘body’]); }
// すべての記事をSELECT $sql = “SELECT * FROM kiji”; $stmt =
$pdo->query($sql); $rows = $stmt->fetchAll(); foreach ($stmt as $kiji) { // 記事の本文を更新する関数 update($kiji[’id’], $kiji[‘body’]); }
$ php update_kiji.php
$ php update_kiji.php PHP Fatal error: Allowed memory size of
134217728 bytes exhausted PHP Stack trace: PHP 1. update_kiji.php PHP 2. PDO->query()
PHP Fatal error: Allowed memory size of 134217728 bytes exhausted
None
$OPT = [PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => false];
PHP Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 2014 Cannot
execute queries while other unbuffered queries are active.
None
// すべての記事をSELECT ini_set(“memory_limit”, “1G”); $sql = “SELECT id FROM kiji”;
$stmt = $pdo->query($sql); foreach ($stmt as $kiji) { // 記事の本文をSELECTして更新する関数 update($kiji[’id’], $kiji[‘body’]); }
常識…?
None