Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $user = "root";
- $pass = "";
- try {
- // MAKE CONNECTION TO OLD DATABASE
- $dbold = new PDO('mysql:host=localhost;dbname=old', $user, $pass);
- $userQuery = $dbold->prepare('SELECT * FROM users');
- $userQuery->execute();
- $users = $userQuery->fetchAll(PDO::FETCH_OBJ);
- // MAKE CONNECTION TO NEW DATABASE
- $dbnew = new PDO('mysql:host=localhost;dbname=new', $user, $pass);
- foreach($users as $user) {
- // TO NEW ADRESS
- $insertAdress = $dbnew->prepare('INSERT INTO addresses (street, house_number, postcode) VALUES (:street, :house_number, :postcode)');
- $insertAdress->bindParam(':street', $user->street);
- $insertAdress->bindParam(':house_number', $user->house_number);
- $insertAdress->bindParam(':postcode', $user->postcode);
- $insertAdress->execute();
- $addressID = $dbnew->lastInsertId();
- // TO NEW PROFILE
- $insertProfile = $dbnew->prepare('INSERT INTO profiles (first_name, last_name) VALUES (:first_name, :last_name)');
- $name = explode(" ", $user->name);
- $insertProfile->bindParam(':first_name', $name[0]);
- $insertProfile->bindParam(':last_name', $name[1]);
- $insertProfile->execute();
- $profileID = $dbnew->lastInsertId();
- // ADD NEW ROLES
- $queryRole = $dbnew->prepare('SELECT COUNT(*) FROM roles WHERE name=:name');
- $queryRole->bindParam(":name", $user->role);
- $queryRole->execute();
- $countRole = $queryRole->fetchColumn();
- if ($countRole == 1) {
- $selectRole = $dbnew->prepare('SELECT * FROM roles where name=:name');
- $selectRole->bindParam(":name", $user->role);
- $selectRole->execute();
- $role = $selectRole->fetch(PDO::FETCH_OBJ);
- $roleID = $role->id;
- } else {
- $insertRole = $dbnew->prepare('INSERT INTO roles (name) VALUES (:name)');
- $insertRole->bindParam(':name', $user->role);
- $insertRole->execute();
- $roleID = $dbnew->lastInsertId();
- }
- // TO NEW USER
- $insertUser = $dbnew->prepare('INSERT INTO users (email, Profile_id, Address_id, Role_id, password) VALUES (:email, :Profile_id, :Address_id, :Role_id, :password)');
- $insertUser->bindParam(':email', $user->email);
- $insertUser->bindParam(':password', $user->password);
- $insertUser->bindParam(':Profile_id', $profileID);
- $insertUser->bindParam(':Address_id', $addressID);
- $insertUser->bindParam(':Role_id', $roleID);
- $insertUser->execute();
- $userID = $dbnew->lastInsertId();
- // BLOGS
- $blogQuery = $dbold->prepare('SELECT * from blog');
- $blogQuery->execute();
- $blogs = $blogQuery->fetchAll(PDO::FETCH_OBJ);
- foreach ($blogs as $blog){
- // TO NEW BLOGS
- $insertBlogs = $dbnew->prepare('INSERT INTO blogs (title, content, User_id) VALUES (:title, :content, :User_id)');
- $insertBlogs->bindParam(':title', $blog->title);
- $insertBlogs->bindParam(':content', $blog->content);
- $insertBlogs->bindParam(':User_id', $blog->Users_id);
- $insertBlogs->execute();
- $blogID = $dbnew->lastInsertId();
- // COMMENTS
- $commentQuery = $dbold->prepare('SELECT * from comment WHERE Blog_id=:Blog_id');
- $commentQuery->bindParam(':Blog_id', $blog->id);
- $commentQuery->execute();
- $comments = $commentQuery->fetchAll(PDO::FETCH_OBJ);
- foreach ($comments as $comment) {
- $insertComments = $dbnew->prepare('INSERT INTO comments (text, Blog_id, User_id) VALUES (:text, :Blog_id, :User_id)');
- $insertComments->bindParam(':text', $comment->text);
- $insertComments->bindParam(':User_id', $userID);
- $insertComments->bindParam(':Blog_id', $blogID);
- $insertComments->execute();
- }
- }
- // FILES
- $fileQuery = $dbold->prepare('SELECT * from file WHERE uploaded_by=:name');
- $fileQuery->bindParam(':name', $user->name);
- $fileQuery->execute();
- $files = $fileQuery->fetchAll(PDO::FETCH_OBJ);
- foreach ($files as $file){
- // TO NEW FILES
- $insertFiles = $dbnew->prepare('INSERT INTO files (filename, User_id) VALUES (:filename, :User_id)');
- $insertFiles->bindParam(':filename', $file->filename);
- $insertFiles->bindParam(':User_id', $userID);
- $insertFiles->execute();
- }
- }
- } catch (PDOException $e) {
- print "Error!: " . $e->getMessage() . "<br/>";
- die();
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement