Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function out($line = '')
- {
- echo PHP_EOL.$line.PHP_EOL;
- }
- function sep()
- {
- out();
- out('------------------------------');
- out();
- }
- /**
- * @param $pdo PDO
- * @param $config array
- */
- function run($pdo, $config)
- {
- $sql = "TRUNCATE products";
- $stmt = $pdo->prepare($sql);
- $stmt->execute();
- $numberOfInsertOps = $config['insert_number'];
- $startInsertTime = microtime(true);
- $sql = "INSERT INTO products (name, price) VALUES (:name, :price)";
- $stmt = $pdo->prepare($sql);
- for ($i = 0; $i < $numberOfInsertOps; $i++)
- {
- $name = md5(rand(0, 1000));
- $price = rand(1, 1000);
- $stmt->bindParam(':name', $name);
- $stmt->bindParam(':price', $price);
- try
- {
- $result = $stmt->execute();
- if (!$result)
- {
- out('shit happened');
- $hostname = 'localhost';
- $username = 'root';
- $password = 'root';
- $db_name = 'test';
- $pdo = new PDO("mysql:host=$hostname;dbname=$db_name", $username, $password);
- $sql = "INSERT INTO products (name, price) VALUES (:name, :price)";
- $stmt = $pdo->prepare($sql);
- $stmt->bindParam(':name', $name);
- $stmt->bindParam(':price', $price);
- $result = $stmt->execute();
- if (!$result)
- {
- out('double shit');
- }
- }
- out($pdo->lastInsertId());
- }
- catch (Exception $e)
- {
- sep($e->getMessage());
- }
- }
- out('Insert in one query time: ' . ( microtime(true) - $startInsertTime) );
- }
- $pdo = new PDO("mysql:host=$hostname;dbname=$db_name", $username, $password);
- run($pdo, array(
- 'insert_number' => 100
- ));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement