How to connect two databases(PHP , MySQL)

by 14 replies
17
Hello warriors , i am trying to make a forum-not real one just for fun , I am learning some PHP and other stuff , and i wanna seprate two databases like one will store all data of user like id , username , password , email , city , last login , profile image and other stuff , and on other one i would like to store id of user and his replay when he replay on something or make a new thread , can anyone help me ty
#programming #connect #databasesphp #mysql
  • Use one database with multiple tables to separate data
    • [ 1 ] Thanks
    • [1] reply
    • Hi Smismi,

      The way a database works is it is split into multiple tables. Generally the way a forum works is it will hold any text in a table with a unique ID (even WF does it, the post ID of this is 9299917) So there is no difference so far between a new topic and a reply. Then you will have another column in this table with "parent" - this will tell you whether it's a new post, or a reply to someone elses post.

      Something like this:
      In your users table:
      userID
      username
      password: (hashed password that no-one should see here)
      email
      etc etc, all as columns of the users table.

      Posts table:
      PostID:
      UserID: (user who posted it)
      Parent: (this could be a reply to another post, if there is nothing here we assume it's a new topic)
      Title
      Content:

      So say I'm userID 78, and I reply to you and you have made a post earlier with PostID 20:

      In the users table:
      userID: 78
      username: Shadoware
      password: fjiurteyhf7853 (encrypted)
      email: info@shadoware.net

      userID: 77
      username: TestAccount
      password: fjiurteyhf7853 (encrypted)
      email: test@shadoware.net

      The in your posts table:

      PostID: 20
      UserID: 77
      Parent:
      Title: New Topic
      Content: Hi this is a topic, you can tell because there is no parent!

      PostID: 21
      UserID: 78
      Parent: 20
      Title: RE: New Topic
      Content: Hi this is a reply! You can tell because my parent is postID 20.

      Then in your PHP, you'd load in all the posts without a parent (SELECT title FROM posts WHERE Parent IS NULL) to give you a topic list, then when you click on a topic, load in any with that parent and display them in topic format. Taking any information you need from the users table too.
      • [ 1 ] Thanks
  • Site will load quicker from one database
    • [ 1 ] Thanks
  • Thank You Shadoware i am new in PHP so i am still learning, i just needed that information because i can understand everything else
    • [1] reply
    • No problem, what you're doing is some basic database normalisation. This ensures you don't duplicate or have un-needed data in your tables, and often helps with linking (we have the User ID in the posts table for example)

      If you look up some simple database normalisation techniques I'm sure you will get the hang of it. (Some tutorials make it more complex and abstract than others, I prefer to just stick to it as a practical guideline)
      • [1] reply
  • Look up "federated tables" for MySQL.
    • [1] reply
    • While this appears to be a very reasonable answer to the OP's actual question, what many of us here are suggesting is that the best approach is to use multiple tables in a single database, rather than using multiple databases.
      • [1] reply
  • Whats the point of two databases?
    As somebody said there already, two tables and one database.
    If you need to use data from 2 tables at same time, use JOIN queries.
    • [ 1 ] Thanks
    • [1] reply
    • Yea i need to use data from 2 tables at same time and i will try to use JOIN queries... Thank You very much...
  • PHP and database design are two very different subjects. I would argue that database design is much more complicated. It's an interesting subject and a must if you want to make any sort of web app. Good luck with your adventure and don't skimp on the database lectures, they will save you from allot of screaming fits in the future.
  • Banned
    [DELETED]
  • Hi,

    Other warriors have given solution your question. But my question is why do you need to have two separate database ? Use one database and create two tables and store the data. so that you can create relationship between tables and access the data very easily. Keeping two database is not needed.

    cheers
    Dhana
  • You can use multiple tables or schemas, there is no need to use different databases, you can separate them to spread I/O load.

Next Topics on Trending Feed

  • 17

    Hello warriors , i am trying to make a forum-not real one just for fun , I am learning some PHP and other stuff , and i wanna seprate two databases like one will store all data of user like id , username , password , email , city , last login , profile image and other stuff , and on other one i would like to store id of user and his replay when he replay on something or make a new thread , can anyone help me ty