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
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
らいあ
February 19, 2017
Programming
150
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
PHPとDBの常識的な話
らいあ
February 19, 2017
More Decks by らいあ
See All by らいあ
GoのDBライブラリについて調べてみた
ryer
0
620
shimanego3-lt.pdf
ryer
0
450
PageSpeed基礎知識
ryer
0
67
PHPひとめぐり
ryer
0
45
関西ソーシャルゲーム勉強会のご紹介
ryer
0
62
Other Decks in Programming
See All in Programming
20260623_Loop Engineeringで自分の分身の問い合わせBotを作る
ryugen04
0
220
分散システム、なんですぐ死んでしまうん?耐障害性を高めたいあなたのためのレジリエンスパターン入門
mshibuya
7
6.4k
琵琶湖の水は止められてもNet--HTTPのリトライは止められない / You might be able to stop the water flow of Lake Biwa but you can't stop Net::HTTP retries
luccafort
PRO
0
400
Hatena Engineer Seminar #37「言語モデルの活用に関する研究」
slashnephy
0
520
そのテスト、説明できますか?~LWテスト戦略FW~のご紹介
nakahara
0
210
AI時代の仕事技芸論〜ソフトウェア開発で「遊ぶように働く」職人的熟達のすすめ(スクフェス仙台 2026バージョン)
kuranuki
0
650
霧の中の代数的エフェクト
funnyycat
1
400
継続モナドとリアクティブプログラミング
yukikurage
3
610
Embedded SREと共に達成した会員管理システムのAWS移行 - SRE NEXT 2026 ランチスポンサーセッション
niftycorp
PRO
1
2.7k
共通化で考えるべきは、実装より公開する型だった
codeegg
0
250
PHP初心者セッション2026 〜生成AIでは見えない裏側を知る:今だからLAMPを通して仕組みを学ぶ〜
kashioka
0
510
地域 SRE コミュニティ最前線 - ホンマでっかSRE勉強会
tk3fftk
0
250
Featured
See All Featured
Beyond borders and beyond the search box: How to win the global "messy middle" with AI-driven SEO
davidcarrasco
3
180
Site-Speed That Sticks
csswizardry
13
1.3k
JAMstack: Web Apps at Ludicrous Speed - All Things Open 2022
reverentgeek
1
500
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3.5k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
230
23k
Max Prin - Stacking Signals: How International SEO Comes Together (And Falls Apart)
techseoconnect
PRO
0
200
Odyssey Design
rkendrick25
PRO
2
730
The Impact of AI in SEO - AI Overviews June 2024 Edition
aleyda
5
1.1k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
254
22k
Code Review Best Practice
trishagee
74
20k
Game over? The fight for quality and originality in the time of robots
wayneb77
1
220
Organizational Design Perspectives: An Ontology of Organizational Design Elements
kimpetersen
PRO
1
770
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