Accessing two databases from one form?

Discussion in 'PHP' started by Matt Ridge, Dec 21, 2011.

  1. #1
    Ok, I am looking to use php to access two databases.

    The form posts into database 1 (person), while I have in the form two drop down menus that access a second database named (pulldown) to populate themselves.

    So it would look like this.

    Now I know to access one database you need to enter in

    $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
    Code (markup):
    How does it work when you need to access two of them?

    Now the pull down code isn't an issue, but it is when it is inserted into a form like what I want, how I want to do it, it is.

    Can anyone here help?

    So what I want is to have access to person and pulldown at the same time, even though data is being pulled from pulldown and entered into person.

    Does this make any sense?
     
    Matt Ridge, Dec 21, 2011 IP
  2. codecre8r

    codecre8r Well-Known Member

    Messages:
    148
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    128
    #2
    You will need to make another database connection.

    For example:

    To access Database 1
    $db1 = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
    $result1 = mysql_query("SELECT * FROM people", $db1);
    Code (markup):
    To access Database 2
    $db2 = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
    $result2 = mysql_query("SELECT * FROM info", $db2);
    Code (markup):
    It would be better if you had both tables in the same database, but I realize that's not possible always.
     
    codecre8r, Dec 21, 2011 IP
  3. Javed iqbal

    Javed iqbal Well-Known Member

    Messages:
    445
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    103
    #3
    he is correct
     
    Javed iqbal, Dec 21, 2011 IP
  4. Matt Ridge

    Matt Ridge Peon

    Messages:
    166
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #4

    How will the Php code know where to pull the data from?

    The reason for not having them in the same database is simple, one will only have data entered to enter a new name, the other will have data entered, changed, removed, etc continuously, I don't want to have the two databases messed up... thanks for the help.
     
    Matt Ridge, Dec 21, 2011 IP
  5. JohnnySchultz

    JohnnySchultz Peon

    Messages:
    277
    Likes Received:
    4
    Best Answers:
    7
    Trophy Points:
    0
    #5
    it's by the $db1 and $db2 variables..

    if you want to pull data from database2 use $db2 in mysql_query..
    don't forget to put it in the second parameter..
     
    JohnnySchultz, Dec 22, 2011 IP