zzarbi

Transfer 100k to Mongo

Feb 2nd, 2012
437
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.82 KB | None | 0 0
  1. <?php $m = new Mongo("mongodb://localhost/mongo");
  2.  
  3. $host = 'localhost';
  4. $dbname = 'mongo';
  5. $user = 'root';
  6. $password = '';
  7.  
  8. $mysqli = new mysqli($host, $user, $password, $dbname);
  9. if ($mysqli->connect_errno){
  10.     echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
  11.     exit;
  12. }
  13.  
  14. $limit = 10000;
  15. $offset = 0;
  16. while(true){
  17.     $res = $mysqli->query("SELECT * FROM basic ORDER BY id ASC LIMIT $offset, $limit");
  18.  
  19.     if($res->num_rows > 0){
  20.         echo "Result set order... $offset, $limit \n";
  21.         while ($row = $res->fetch_assoc()) {
  22.             $o = new stdClass();
  23.             $o->_id = $row['id'];
  24.             $o->text = $row['text'];
  25.             echo " id = " . $row['id'] . " . text = ".$row['text'].PHP_EOL;
  26.             $m->mongo->basic->insert($o);
  27.         }
  28.  
  29.         $offset += $limit;
  30.     }else{
  31.         echo 'Done'.PHP_EOL;
  32.         die;
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment