Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8799481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:20:07+00:00 2026-06-14T00:20:07+00:00

I’m writing a single PHP script to migrate topics from an old forums site

  • 0

I’m writing a single PHP script to migrate topics from an old forums site to a new one.

  • The old forums site use database “old_forums”
  • Thew new forums site use database “new_forums”
  • MySQL user “forums” has all privileges to both databases (I’m using 1 single user for convenience but I would not have any problems using 2 different users if required)

I have both forum hosted on the the same host – localhost

The script I have the following structure

<?php  
class Forum {  
   //constants  
   const HOST = "localhost";  
   const DB_USER = "forums";  
   const DB_PASS = "forums";  
   ...  
   //properties e.g. topic title, topic content
}
class OldForum extends Forum {  
   const DB_NAME_OLD = "old_forums";  
   public static function exportTopics() {  
       //some code
   }  
}
class NewForum extends Forum {
   const DB_NAME_NEW = "new_forums";
   public static function importTopics() {  
       //some code
   }
}  
OldForum::exportTopics();  
NewForum::importTopics();  
?>   

I understand that I’m mixing procedural & object-oriented programming PHP (OOPP) here.
I’m new to object-oriented PHP but (I have experience with Java so I’m very open to some guide to make this pure OOPP)

I would like to ultilise 1 single MySQL connection for both OldForum and NewForum class.
Where should I instantiate a mysqli object?
e.g. inside Forum class’ constructor or have a new mysqli object as a property of class Forum
so that I would create a new Forum object to initiate a MySQL connection

$a_forum = new Forum();  
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T00:20:08+00:00Added an answer on June 14, 2026 at 12:20 am

    The mysqli connection is easy enough to share between instances by creating it once in your bootstrap file and then passing it to instances that need it, e.g.

    $mysqli = new mysqli(/* connection params */);
    $someClassUsingMySqli = new SomeClassUsingMySqli($mysqli);
    $anotherClassUsingMySqli= new AnotherClassUsingMySqli($mysqli);
    

    That will effectively limit the connection to one and you dont need to resort to globals inside your objects. This is called Dependency Injection and should be your prefered way of assigning dependencies to objects. It makes dependencies explicit and easy to swap out and thus benefits change, testing and maintenance.

    As for your Import and Export Task, I wonder why you are doing this in PHP at all. It’s apparently the same database server, so you could just do it inside your MySql instance. If you want to do it with PHP, I’d probably do something like this:

    class MigrateForum
    {
        private $dbConnector;
    
        public function __construct(DBConnector $dbConnector)
        {
            $this->dbConnector = $dbConnector;
        }
    
        public function migrate()
        {
            // orchestrate the migration (consider transactions)
            $this->exportOldForum();
            $this->importNewForum();
        }
    
        private function exportOldForum()
        {
            // code to export old_database_name.table_name 
        }
    
        private function importOldForum()
        {
            // code to import new_database_name.table_name 
        }
    }
    

    You could extract the Import and Export methods into their own Classes and then use some sort of Composite Command Pattern, but that really depends on how modular you need this to be.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have a view passing on information from a database: def serve_article(request, id): served_article
I'm trying to create an if statement in PHP that prevents a single post
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.