Upgrade to Pro — share decks privately, control downloads, hide ads and more …

PHPとDBの常識的な話

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.
Avatar for らいあ らいあ
February 19, 2017

 PHPとDBの常識的な話

Avatar for らいあ

らいあ

February 19, 2017
Tweet

More Decks by らいあ

Other Decks in Programming

Transcript

  1. $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);
  2. $sql = “SELECT * FROM prefs WHERE id =:id”; $stmt

    = $pdo->prepare($sql); $stmt->execute([‘id’ => 123]); $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); var_dump($rows);
  3. // すべての記事をSELECT $sql = “SELECT * FROM kiji”; $stmt =

    $pdo->query($sql); $rows = $stmt->fetchAll(); foreach ($rows as $kiji) { // 記事の本文を更新する関数 update($kiji[’id’], $kiji[‘body’]); }
  4. // すべての記事をSELECT $sql = “SELECT * FROM kiji”; $stmt =

    $pdo->query($sql); $rows = $stmt->fetchAll(); foreach ($stmt as $kiji) { // 記事の本文を更新する関数 update($kiji[’id’], $kiji[‘body’]); }
  5. $ 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()
  6. PHP Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 2014 Cannot

    execute queries while other unbuffered queries are active.
  7. // すべての記事を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’]); }