[英]taking data from different tables and different dbs
I want to retrieve two different fields residing in different data bases. 我想检索驻留在不同数据库中的两个不同字段。 My db connection settings are this:
我的数据库连接设置是这样的:
define ('DB_HOST', 'ipaddress1');
define ('DB_USER', 'username1');
define ('DB_PASSWORD', 'password1');
define ('DB_DATABASE', 'ecbooks');
$db_wink = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD, TRUE) or die("Connection Error: " . mysql_error());
mysql_select_db(DB_DATABASE) or die("Error connecting to Winkstore DB. " . mysql_error());
// DB configuration parameters : magonwink
define ('DB_REMOTE_HOST', 'ipaddress2');
define ('DB_REMOTE_USER', 'username2');
define ('DB_REMOTE_PASSWORD', 'password2');
define ('DB_REMOTE_DATABASE', 'magsonwink');
$db_magson = mysql_connect(DB_REMOTE_HOST, DB_REMOTE_USER, DB_REMOTE_PASSWORD, TRUE) or die("Connection Error: " . mysql_error());
mysql_select_db(DB_REMOTE_DATABASE, $db_magson) or die("Error connecting to magson wink DB. " . mysql_error());
define ('CMS_DB_HOST', 'ipaddress3');
define ('CMS_DB_USER', 'username3');
define ('CMS_DB_PASSWORD', 'password3');
define ('CMS_DB_DATABASE', 'mawinkcms');
$db_rp = mysql_connect(CMS_DB_HOST, CMS_DB_USER, CMS_DB_PASSWORD, true) or die("Connection Error: " . mysql_error());
mysql_select_db(CMS_DB_DATABASE, $db_rp) or die("Error connecting to DB. " . mysql_error());
but when I used this query 但是当我使用这个查询
SELECT ecbooks.user.user_name AS field1, mawinkcms.purchase.USER_NAME AS field2 FROM ecbooks.user,mawinkcms.purchase
$result = mysql_query($query, $db_wink) or die("Couldn't execute query: " . mysql_error());
while($row = mysql_fetch_assoc($result)){
$users[] = $row;
}
I got an error could not execute the query. 我收到一个错误,无法执行查询。 Thanks in advance.
提前致谢。
You have to pass the correct link identifier to the mysql_query
function as the second parameter: http://www.php.net/mysql_query 您必须将正确的链接标识符传递给
mysql_query
函数作为第二个参数: http : //www.php.net/mysql_query
$ecbooks
does not exist (based on the code you provided). $ecbooks
不存在(根据您提供的代码)。
In your case if the table you want to access is in the ecbooks
database, you have to pass $db_wink
to mysql_query
. 如果您要访问的表位于
ecbooks
数据库中,则必须将$db_wink
传递给mysql_query
。
But since you are trying to access two different databases in the same query, you will have to connect to the database with a user that has access to both databases. 但是,由于您试图在同一查询中访问两个不同的数据库,因此您将必须使用有权访问两个数据库的用户连接到该数据库。 Posting the actual error you get would also help.
发布您得到的实际错误也有帮助。
ps: please stop usingng this extension to access MySQL. ps:请停止使用此扩展名来访问MySQL。 See here for details.
有关详细信息,请参见此处 。
Firstly, when asking for help with an error, it's a good idea to include the error itself. 首先,在寻求有关错误的帮助时,最好将错误本身包括在内。
Secondly, your query doesn't appear to have a join condition, so you will get back the cartesian product if you do get it to go - that's probably not what you want. 其次,您的查询似乎没有连接条件,因此,如果您可以使用笛卡尔积,则可以取回它-这可能不是您想要的。
Thirdly, when running a query, it runs against the database you connected to, regardless of whether you've connected to any others. 第三,在运行查询时,无论您是否已连接到其他数据库,查询都会针对您所连接的数据库运行。 So, in your example, you're running a query against $db_wink;
因此,在您的示例中,您正在对$ db_wink运行查询; the other two connections don't affect this at all.
其他两个连接根本不影响此操作。
For the query to work, the user who connected to $db_wink needs to have permissions on the databases ecbooks and mawinkcms, and mawinkcms must be running on the same server as ecbooks. 为了使查询正常工作,连接到$ db_wink的用户需要对数据库ecbook和mawinkcms具有权限,并且mawinkcms必须与ecbook在同一服务器上运行。
I'm guessing that either the permissions are not set up, or you're running them on different databases. 我猜测没有设置权限,或者您正在不同的数据库上运行它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.