daily pastebin goal
2%
SHARE
TWEET

Untitled

a guest Mar 16th, 2018 301 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. $username = 'root';
  3. $password = '';
  4.  
  5. $oldDatabaseName = 'old2';
  6.  
  7. try {
  8.     // 1. Make Connections
  9.     $old = new PDO('mysql:host=127.0.0.1;dbname=old2', $username, $password);
  10.     $new = new PDO('mysql:host=127.0.0.1;dbname=new2', $username, $password);
  11.     // 2. Set Error Mode && Exception Mode
  12.     $old->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  13.    
  14.     output('Connected to ' . $oldDatabaseName);
  15. } catch (PDOException $e) {
  16.     output("Connection Failed: " . $e->getMessage());
  17.     exit;
  18. }
  19.  
  20.  
  21. // 3. Prepare SQL Query
  22. $getAccounts = $old->prepare('SELECT * FROM accounts');
  23. // 4. Execute SQL Query
  24. $getAccounts->execute();
  25. // 5. Fetch all accounts from executed SQL Query
  26. $accounts = $getAccounts->fetchAll(PDO::FETCH_ASSOC);
  27. // 6. Dump foreach all accounts
  28. $roles = [];
  29.  
  30. if($accounts) {
  31.     foreach ($accounts as $account) {
  32.         array_push($roles, $account['role']);
  33.     }
  34.  
  35.     $roles = array_unique($roles);
  36.  
  37.     foreach($roles as $role) {
  38.        
  39.         $getRoles = $new->prepare("SELECT name FROM roles WHERE name = :role");
  40.  
  41.         $getRoles->execute([
  42.             ':role' => 'Adminasd'
  43.         ]);
  44.  
  45.         $roles = $getRoles->fetch();
  46.  
  47.         var_dump($roles);
  48.         exit;
  49.  
  50.         $insertRoles = $new->prepare("INSERT INTO `roles` (name) VALUES (:name)");
  51.  
  52.         $insertRoles->execute([
  53.             ':name' => $role
  54.         ]);
  55.     }
  56.  
  57.     output(count($roles) . ' roles transfered.');
  58.  
  59.     foreach ($accounts as $account) {
  60.         // 7. Prepare SQL Query
  61.         $insertAccounts = $new->prepare("INSERT INTO `accounts` (email, password, role_id) VALUES (:email, :password, :role_id)");
  62.  
  63.         switch($account['role']) {
  64.             case 'Bezoeker':
  65.                 $roleID = 19;
  66.                 break;
  67.             case 'Medewerker':
  68.                 $roleID = 20;
  69.                 break;
  70.             case 'Admin':
  71.                 $roleID = 21;
  72.                 break;
  73.         }
  74.  
  75.         $insertAccounts->execute([
  76.             ':email' => 1,
  77.             ':password' => 2,
  78.             ':role_id' => 3
  79.         ]);
  80.     }
  81.  
  82.     output(count($accounts) . ' accounts transfered.');
  83.  
  84. } else {
  85.     output('No accounts found.');
  86.     exit;
  87. }
  88.  
  89. $getRecipes = $old->prepare("SELECT * FROM recipes");
  90. $getRecipes->execute();
  91.  
  92. $recipes = $getIngredients->execute();
  93.  
  94. if($recipes) {
  95.  
  96.     foreach ($recipes as $recipe) {
  97.         var_dump($recipe);
  98.     }
  99.  
  100.  
  101. } else {
  102.     output('No recipes found.');
  103.     exit;
  104. }
  105.  
  106. function output($output){
  107.     echo $output . "\n";
  108. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top