zzarbi

Insert 100k in Mysql

Feb 2nd, 2012
475
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.74 KB | None | 0 0
  1. <?php
  2. $host = 'localhost';
  3. $dbname = 'mongo';
  4. $user = 'root';
  5. $password = '';
  6.  
  7. $mysqli = new mysqli($host, $user, $password, $dbname);
  8. if ($mysqli->connect_errno){
  9.     echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
  10.     exit;
  11. }
  12.  
  13. $start = microtime(true);
  14. $i = $total = 100000;
  15. while($i--){
  16.     $j = rand(5, 25);
  17.     $text = '';
  18.     while($j--){
  19.         $text .= chr(rand(97, 122));
  20.     }
  21.     $id = $total - $i;
  22.     echo 'Add '.$id.' => '.$text.PHP_EOL;
  23.     $sql = 'INSERT INTO `basic` (`text`) VALUES (\''.$text.'\');';
  24.     if(!$mysqli->query($sql)){
  25.         echo "Insert failed: (" . $mysqli->errno . ") " . $mysqli->error;
  26.         die;
  27.     }
  28. }
  29. $end = microtime(true);
  30. $result = $end - $start;
  31. echo "Done in $result seconds".PHP_EOL;
Advertisement
Add Comment
Please, Sign In to add comment