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
130
PHPとDBの常識的な話
らいあ
February 19, 2017
Tweet
Share
More Decks by らいあ
See All by らいあ
GoのDBライブラリについて調べてみた
ryer
0
560
shimanego3-lt.pdf
ryer
0
380
PageSpeed基礎知識
ryer
0
55
PHPひとめぐり
ryer
0
35
関西ソーシャルゲーム勉強会のご紹介
ryer
0
49
Other Decks in Programming
See All in Programming
盆栽転じて家具となる / Bonsai and Furnitures
aereal
0
1.8k
ドメインイベント増えすぎ問題
h0r15h0
2
560
20241217 競争力強化とビジネス価値創出への挑戦:モノタロウのシステムモダナイズ、開発組織の進化と今後の展望
monotaro
PRO
0
280
PicoRubyと暮らす、シェアハウスハック
ryosk7
0
200
為你自己學 Python
eddie
0
510
サーバーゆる勉強会 DBMS の仕組み編
kj455
1
300
ErdMap: Thinking about a map for Rails applications
makicamel
1
570
PSR-15 はあなたのための ものではない? - phpcon2024
myamagishi
0
400
週次リリースを実現するための グローバルアプリ開発
tera_ny
1
1.1k
rails newと同時に型を書く
aki19035vc
5
710
Alba: Why, How and What's So Interesting
okuramasafumi
0
200
LLM Supervised Fine-tuningの理論と実践
datanalyticslabo
8
1.9k
Featured
See All Featured
Agile that works and the tools we love
rasmusluckow
328
21k
Visualization
eitanlees
146
15k
For a Future-Friendly Web
brad_frost
176
9.5k
GraphQLの誤解/rethinking-graphql
sonatard
68
10k
How to Ace a Technical Interview
jacobian
276
23k
A better future with KSS
kneath
238
17k
GraphQLとの向き合い方2022年版
quramy
44
13k
Code Review Best Practice
trishagee
65
17k
Speed Design
sergeychernyshev
25
730
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
10
860
Mobile First: as difficult as doing things right
swwweet
222
9k
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
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