简体   繁体   中英

How to display data from two different databases in MySQL w/php

I have a customer that wants to add emails that I are stored in a database table of a content management software as registered users to an html-generated table from another database that I have created to store all the data for each store. One registered user could have twenty stores. How do I connect that user's email from CMS database to the html table output of enrolled stores that they own that are coming from another database.

The following illustration explains hopefully what is hard to explain in words. 'User' in DB1 is the same as 'id' in DB2 for each store they own - see below:

USER DATABASE (DB1) =====================

USER   |  EMAIL   |  NAME   |  WEBSITE
-----------------------------------------
user1  |  email1  |  name1  |  website 1
user2  |  email2  |  name2  |  website 2
user3  |  email3  |  name3  |  website 3


STORE DATABASE (DB2) ====================

ID     |  STORE    |  CITY    |  STATE
-----------------------------------------
user1  |  store1a  |  city1a  |  state1a
user1  |  store1a  |  city1a  |  state1a
user2  |  store1b  |  city1b  |  state1b
user2  |  store1b  |  city1b  |  state1b
user2  |  store1b  |  city1b  |  state1b
user3  |  store1c  |  city1c  |  state1c
user3  |  store1c  |  city1c  |  state1c

Now I want to produce html table results like this:

ID     |  STORE    |  CITY    |  STATE    |  EMAIL
-------------------------------------------------------
user1  |  store1a  |  city1a  |  state1a  |  email1
user1  |  store1a  |  city1a  |  state1a  |  email1
user2  |  store1b  |  city1b  |  state1b  |  email2
user2  |  store1b  |  city1b  |  state1b  |  email2
user2  |  store1b  |  city1b  |  state1b  |  email2
user3  |  store1c  |  city1c  |  state1c  |  email3
user3  |  store1c  |  city1c  |  state1c  |  email3

As you can see I query DB2 with the hopes of showing the same user's email from DB1 with all the stores the user owns.

This is at the request of the customer so they can contact regarding their enrollment using this html table. I thought I would throw this out there. The cusotmer is only asking if it is possible, that it would be helpful. I just thought this sounded like something someone would love to tackle and I am just curious after having tried inner join or just querying two separate databases and trying to use variables ... to no avail!

Are both databases on the same server? Is the user name and password for the database the same?

If so you should be able to do something like this

SELECT * from databasename2NAME.db2 as t1
LEFT JOIN  datebasename1NAME.db1 as t2
ON t1.id=t2.USER
WHERE STATMENT HERE

databasename2NAME should be replaced with the database name for db2 and .db2 replaced with the table name in that database

same for db1

Since the two databases have different passwords, I imagine you'll need two separate queries. This is how I'd try to do it. Processing power might be a bit much. Maybe someone can speed it up.

$link1 = mysqli_connect($hostname1, $username1, $password1, $database1);
$query1 = "SELECT user,email FROM db1";
if ( $result1 = mysqli_query($link1, $query1, MYSQLI_STORE_RESULT) )
{
    while ( $r1 = mysqli_fetch_array($result1, MYSQLI_ASSOC) )
    {
        $array[$r1['user']] = $r1['email'];
    }

    $link2 = mysqli_connect($hostname2, $username2, $password2, $database2);
    foreach ( $array as $u=>$e )
    {
        $query2 = "SELECT * FROM db2 WHERE id='{$u}'";
        if ( $result2 = mysqli_query($link2, $query2, MYSQLI_STORE_RESULT) )
        {
            echo "<table>";
            while ( $r2 = mysqli_fetch_array($result2, MYSQLI_ASSOC) )
            {
                echo "<tr>";
                echo "<td>{$u}</td>";
                echo "<td>{$r2['store']}</td>";
                echo "<td>{$r2['city']}</td>";
                echo "<td>{$r2['state']}</td>";
                echo "<td>{$e}</td>";
                echo "</tr>";
            }
            echo "</table>";
        }
    }
    mysqli_close($link2);
}
mysqli_close($link1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM