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

Why don't you try "Query Builder"

omoon
May 21, 2016

Why don't you try "Query Builder"

PHPカンファレンス福岡2016のトークで使ったスライドです
関連レポジトリ)https://github.com/omoon/phpconfuk2016

omoon

May 21, 2016
Tweet

More Decks by omoon

Other Decks in Technology

Transcript

  1. $name = 'PHPΤϯδχΞཆ੒ಡຊ'; 
 $sql = "
 SELECT `name`, `price`

    
 FROM `books` 
 WHERE `name` = $name
 ORDER BY `price` DESC ";
 
 $sth = $pdo->prepare($sql);
 $sth->execute();
  2. $name = "t' OR 't' = 't";
 
 $sql =

    <<<'SQL'
 SELECT `name`, `price`
 FROM `books`
 WHERE `name` = $name
 ORDER BY `price` DESC
 SQL;
 
 $sth = $pdo->prepare($sql);
 $sth->execute();
  3. $name = "t' OR 't' = 't";
 
 $sql =

    <<<SQL
 SELECT `name`, `price`
 FROM `books`
 WHERE `name` = ?
 ORDER BY `price` DESC
 SQL;
 
 $sth = $pdo->prepare($sql);
 $sth->execute([$name]);
  4. $name = "t' OR 't' = 't"; 
 $builder->table('books')
 ->select('name',

    'price')
 ->where('name', '=', $name)
 ->orderBy('name', 'desc');
  5. SELECT `name`, `price` 
 FROM `books` 
 WHERE `name` LIKE

    ‘%PHP%' AND `price` > 2000
 ORDER BY `price` DESC 
 LIMIT 10
  6. $sql = "
 SELECT `name`, `price` 
 FROM `books` 


    WHERE `name` LIKE :name
 AND `price` > :price 
 ORDER BY `price` DESC 
 LIMIT :limit 
 ";
 
 $sth = $pdo->prepare($sql);
 $sth->bindParam(':name', $name);
 $sth->bindParam(':price', $price);
 $sth->bindParam(':limit', $limit, PDO::PARAM_INT);
 $sth->execute();
  7. $name = '%PHP%';
 $price = 2000;
 $limit = 10; 


    $sql = "SELECT `name`, `price` 
 FROM `books` 
 WHERE `name` LIKE ? AND `price` > ?
 ORDER BY `price` DESC 
 LIMIT ?";
 
 $sth = $pdo->prepare($sql);
 $sth->execute([$name, $price, $limit]);
  8. $name = '%PHP%';
 $price = 2000; $limit = 10;
 


    $builder->table('books')
 ->select('name', 'price')
 ->where('name', 'like', $name)
 ->where('price', '>', $price)
 ->orderBy('name', 'desc')
 ->take($limit);
  9. use Illuminate\Database\Capsule\Manager as Builder; class Connection
 {
 private $host =

    'localhost';
 private $username = 'user';
 private $password = 'pass';
 private $database = 'database';
 public function getBuilder()
 {
 $builder = new Builder();
 $builder->addConnection([
 'driver' => 'mysql',
 'host' => $this->host,
 'database' => $this->database,
 'username' => $this->username,
 'password' => $this->password,
 'charset' => 'utf8',
 'collation' => 'utf8_unicode_ci',
 'prefix' => '',
 ]);
 $builder->setAsGlobal();
 $builder->setFetchMode(PDO::FETCH_ASSOC);
 return $builder->connection();
 }
 } $builder = (new Connection())->getBuilder();
  10. SELECT
 `books`.`name` AS `book_name`,
 `books`.`price`,
 `authors`.`name` AS `author_name`,
 `authors`.`age`
 FROM

    `books` LEFT JOIN `authors`
 ON `books`.`author_id` = `authors`.`id`
 WHERE `books`.`price` >= ?
  11. $results = $builder->table('books')
 ->leftJoin('authors', 'books.author_id', '=', 'authors.id')
 ->where('books.price', '>=', 5000)


    ->select(
 'books.name as book_name',
 'books.price',
 'authors.name as author_name',
 'authors.age'
 );
  12. $values = [];
 for ($i = 0; $i < 10000;

    $i++) {
 $values[] = [
 'name' => 'name' . $i,
 'author_id' => rand(1, 10),
 'price' => rand(500, 10000),
 ];
 }
 
 $sql = "INSERT INTO books (name, author_id, price) values (?, ?, ?)”; 
 $sth = $pdo->prepare($sql);
 foreach ($values as $value) {
 $sth->execute(array_values($value));
 }
 
 $builder->table('books')->insert($values);
  13. INSERT INTO `books`(`name`, `author`, `price`) VALUES(?, ?, ?)
 .
 .


    .
 VS 
 INSERT INTO `books`(`name`, `author`, `price`) VALUES(?, ?, ?),(?, ?, ?),(?, ?, ?)......