Advertisement
Guest User

Untitled

a guest
May 2nd, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. <?php
  2. $user = "root";
  3. $pass = "";
  4. try {
  5. // MAKE CONNECTION TO OLD DATABASE
  6. $dbold = new PDO('mysql:host=localhost;dbname=old', $user, $pass);
  7. $userQuery = $dbold->prepare('SELECT * FROM users');
  8. $userQuery->execute();
  9. $users = $userQuery->fetchAll(PDO::FETCH_OBJ);
  10. // MAKE CONNECTION TO NEW DATABASE
  11. $dbnew = new PDO('mysql:host=localhost;dbname=new', $user, $pass);
  12. foreach($users as $user) {
  13. // TO NEW ADRESS
  14. $insertAdress = $dbnew->prepare('INSERT INTO addresses (street, house_number, postcode) VALUES (:street, :house_number, :postcode)');
  15. $insertAdress->bindParam(':street', $user->street);
  16. $insertAdress->bindParam(':house_number', $user->house_number);
  17. $insertAdress->bindParam(':postcode', $user->postcode);
  18. $insertAdress->execute();
  19. $addressID = $dbnew->lastInsertId();
  20.  
  21. // TO NEW PROFILE
  22. $insertProfile = $dbnew->prepare('INSERT INTO profiles (first_name, last_name) VALUES (:first_name, :last_name)');
  23. $name = explode(" ", $user->name);
  24. $insertProfile->bindParam(':first_name', $name[0]);
  25. $insertProfile->bindParam(':last_name', $name[1]);
  26. $insertProfile->execute();
  27. $profileID = $dbnew->lastInsertId();
  28.  
  29. // ADD NEW ROLES
  30. $queryRole = $dbnew->prepare('SELECT COUNT(*) FROM roles WHERE name=:name');
  31. $queryRole->bindParam(":name", $user->role);
  32. $queryRole->execute();
  33. $countRole = $queryRole->fetchColumn();
  34. if ($countRole == 1) {
  35. $selectRole = $dbnew->prepare('SELECT * FROM roles where name=:name');
  36. $selectRole->bindParam(":name", $user->role);
  37. $selectRole->execute();
  38. $role = $selectRole->fetch(PDO::FETCH_OBJ);
  39. $roleID = $role->id;
  40. } else {
  41. $insertRole = $dbnew->prepare('INSERT INTO roles (name) VALUES (:name)');
  42. $insertRole->bindParam(':name', $user->role);
  43. $insertRole->execute();
  44. $roleID = $dbnew->lastInsertId();
  45. }
  46.  
  47. // TO NEW USER
  48. $insertUser = $dbnew->prepare('INSERT INTO users (email, Profile_id, Address_id, Role_id, password) VALUES (:email, :Profile_id, :Address_id, :Role_id, :password)');
  49. $insertUser->bindParam(':email', $user->email);
  50. $insertUser->bindParam(':password', $user->password);
  51. $insertUser->bindParam(':Profile_id', $profileID);
  52. $insertUser->bindParam(':Address_id', $addressID);
  53. $insertUser->bindParam(':Role_id', $roleID);
  54. $insertUser->execute();
  55. $userID = $dbnew->lastInsertId();
  56.  
  57. // BLOGS
  58. $blogQuery = $dbold->prepare('SELECT * from blog');
  59. $blogQuery->execute();
  60. $blogs = $blogQuery->fetchAll(PDO::FETCH_OBJ);
  61. foreach ($blogs as $blog){
  62. // TO NEW BLOGS
  63. $insertBlogs = $dbnew->prepare('INSERT INTO blogs (title, content, User_id) VALUES (:title, :content, :User_id)');
  64. $insertBlogs->bindParam(':title', $blog->title);
  65. $insertBlogs->bindParam(':content', $blog->content);
  66. $insertBlogs->bindParam(':User_id', $blog->Users_id);
  67. $insertBlogs->execute();
  68. $blogID = $dbnew->lastInsertId();
  69. // COMMENTS
  70. $commentQuery = $dbold->prepare('SELECT * from comment WHERE Blog_id=:Blog_id');
  71. $commentQuery->bindParam(':Blog_id', $blog->id);
  72. $commentQuery->execute();
  73. $comments = $commentQuery->fetchAll(PDO::FETCH_OBJ);
  74. foreach ($comments as $comment) {
  75. $insertComments = $dbnew->prepare('INSERT INTO comments (text, Blog_id, User_id) VALUES (:text, :Blog_id, :User_id)');
  76. $insertComments->bindParam(':text', $comment->text);
  77. $insertComments->bindParam(':User_id', $userID);
  78. $insertComments->bindParam(':Blog_id', $blogID);
  79. $insertComments->execute();
  80. }
  81. }
  82.  
  83. // FILES
  84. $fileQuery = $dbold->prepare('SELECT * from file WHERE uploaded_by=:name');
  85. $fileQuery->bindParam(':name', $user->name);
  86. $fileQuery->execute();
  87. $files = $fileQuery->fetchAll(PDO::FETCH_OBJ);
  88. foreach ($files as $file){
  89. // TO NEW FILES
  90. $insertFiles = $dbnew->prepare('INSERT INTO files (filename, User_id) VALUES (:filename, :User_id)');
  91. $insertFiles->bindParam(':filename', $file->filename);
  92. $insertFiles->bindParam(':User_id', $userID);
  93. $insertFiles->execute();
  94. }
  95. }
  96. } catch (PDOException $e) {
  97. print "Error!: " . $e->getMessage() . "<br/>";
  98. die();
  99. }
  100.  
  101. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement