How to connect two databases(PHP , MySQL)

by smismi
14 replies
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
#connect #databasesphp #mysql
  • Profile picture of the author kingjpm
    Use one database with multiple tables to separate data
    Signature
    RogueDen.com
    {{ DiscussionBoard.errors[9299917].message }}
    • Profile picture of the author Shadoware
      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.
      {{ DiscussionBoard.errors[9299981].message }}
  • Profile picture of the author Anne Laidlaw
    Site will load quicker from one database
    Signature
    Giant Plugin Biz High Quality WP Plugin Package
    WP Plugins Are Hott!! Claim Your Piece Of The Pie Today!
    Free Squeeze Page Creator - FREE Instant Access To Alou's Killer Squeeze Page Creator.
    Alou.com - Wordpress SEO - Latest free WordPress SEO, Plugins, Themes and more!
    {{ DiscussionBoard.errors[9300334].message }}
  • Profile picture of the author smismi
    Thank You Shadoware i am new in PHP so i am still learning, i just needed that information because i can understand everything else
    {{ DiscussionBoard.errors[9300669].message }}
    • Profile picture of the author Shadoware
      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)
      {{ DiscussionBoard.errors[9300818].message }}
      • Profile picture of the author smismi
        Originally Posted by Shadoware View Post

        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)

        I do not watch any tutorials on Youtube , i am just learning from PHP site because i know that tutorial make it more complex ...
        {{ DiscussionBoard.errors[9300883].message }}
  • Profile picture of the author kpmedia
    Look up "federated tables" for MySQL.
    {{ DiscussionBoard.errors[9303140].message }}
    • Profile picture of the author David Beroff
      Originally Posted by kpmedia View Post

      Look up "federated tables" for MySQL.
      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.
      Signature
      Put MY voice on YOUR video: AwesomeAmericanAudio.com
      {{ DiscussionBoard.errors[9303306].message }}
      • Profile picture of the author kpmedia
        Originally Posted by David Beroff View Post

        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.
        I can think of several real-world applications for federated tables. I've had to do it before. Sometimes having a single database is simply not possible.

        And if this is indeed just practice, it would be something useful to practice.
        {{ DiscussionBoard.errors[9305463].message }}
  • Profile picture of the author Lani
    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.
    {{ DiscussionBoard.errors[9305304].message }}
    • Profile picture of the author smismi
      Originally Posted by Lani View Post

      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.
      Yea i need to use data from 2 tables at same time and i will try to use JOIN queries... Thank You very much...
      {{ DiscussionBoard.errors[9307019].message }}
  • Profile picture of the author pjunction
    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.
    {{ DiscussionBoard.errors[9306215].message }}
  • Profile picture of the author pdhana
    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
    {{ DiscussionBoard.errors[9330286].message }}
  • Profile picture of the author JohnBrower
    You can use multiple tables or schemas, there is no need to use different databases, you can separate them to spread I/O load.
    {{ DiscussionBoard.errors[9342321].message }}

Trending Topics