简体   繁体   English

如何显示来自2个不同数据库的数据

[英]How to Display data from 2 different Database

I have an hard time to retrieve data in a table from MySQL database. 我很难从MySQL数据库中检索表中的数据。 I have 2 different database that cannot be merge but there is a table in the first database that is identical to the second database. 我有2个无法合并的数据库,但是第一个数据库中的表与第二个数据库相同。

Description Database 1 table: areas : ar_id, name, password. 说明数据库1表:区域:ar_id,名称,密码。
Description Database 2 table: user : id, username, pass. 说明数据库2表:用户:ID,用户名,密码。

Now, When the user Login, He logs in the 2nd database. 现在,当用户登录时,他登录第二个数据库。 in each page of the user I have use $_SESSION['username'] to call the username. 在用户的每个页面中,我都使用$_SESSION['username']来调用用户名。

Importantly, In every page, I have table that displays data from different tables using the username in the 2 Database; 重要的是,在每个页面中,我都有一个表,该表使用2数据库中的用户名显示来自不同表的数据。 this else the SQL to be specific and only provide each user with their own information. 这是SQL的具体说明,仅向每个用户提供他们自己的信息。 and That's Ok. 没关系。 This is the SQL: 这是SQL:

SELECT Client_table.Name, Client_table.Client_Id FROM Client_table, user WHERE user.username = '" . $_SESSION['username'] . "' AND Client_table.Branch = user.area Order by Name ASC

In one of the page, I totally using the 1st Database with this SQL to display data in the table : 在其中一个页面中,我完全使用第一个数据库和此SQL在表中显示数据:

select site_id, site_name from sites WHERE srep_id = 5
                        AND status = 1 or status = 2
                        order by site_name asc 

QUESTION: I would like to display this SQL data in a table by using the username or id from the 2nd database BUT is returns Empty Table (I include both Database in this page). 问题:我想通过使用第二个数据库中的用户名或ID在表中显示此SQL数据,但返回空表(我在此页面中同时包含两个数据库)。 This is my current SQL but still not displaying anything: 这是我当前的SQL,但仍然不显示任何内容:

SELECT cl.client_name, st.site_id, st.site_name
                FROM Database1.sites st
                JOIN Database2.user u ON u.id = st.ar_id
                JOIN Database1.clients cl ON cl.client_id = st.client_id
WHERE Database1.st.name = '".$_SESSION['username']."'
                        AND st.status > 0
                        ORDER BY st.site_name ASC

NOTE: This is a major problem that took me almost a week! 注意:这是一个主要问题,我花了将近一周的时间! Please some one help! 请一些帮助!

I think I have an answer. 我想我有一个答案。 After browsing and doing some search, I sound that I can make use of the $_SESSION here and Also, This was my final SQL Statement that Helped me to Connect the 2 Database from the same SQL Statement by using variable in PHP Script. 浏览并进行搜索后,我听起来可以在这里使用$ _SESSION,而且,这是我最终的SQL语句,它帮助我通过使用PHP脚本中的变量来从同一SQL语句连接2数据库。

session_start(); 在session_start();

$result = mysql_query("SELECT cl.client_name, st.site_id, st.site_name, ar.rep_id
                        FROM sites st
                        JOIN areas ar ON ar.rep_id = st.srep_id
                        JOIN clients cl ON cl.client_id = st.client_id
                        WHERE st.srep_id = '".$_SESSION['userarea']."'
                        AND st.status > 0
                        ORDER BY st.site_name ASC");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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