I am using one db file to connect to the database. However, i need to show something else from another db database on this site. I can't use two db connect files. So, how do i go about display one websites content on another website with two different connections.
2 Answers
For one thing, you don't need separate database connections to the same server. Simply specify the database name before each table name in your SQL statements. For example:
SELECT foo, bar FROM db.table;
rather than:
SELECT foo, bar FROM table;
For another, you shouldn't be storing the database connection in a global for just the sort of reason you're running into. I suspect you're using the outdated mysql extension, which uses an implicit DB connection resource whenever one isn't explicit. Switch to PDO and create a simple connection manager class. One big advantage to PDO is it supports prepared statements, which can be safer and more performant than what the mysql extension provides. If you need a PDO tutorial, try "Writing MySQL Scripts with PHP and PDO"
-
And ensure that the user that you're connecting as has appropriate permissions on the extra database. Also, both databases need to be running on the same server (which is probably the case, but not necessarily). More details - http://stackoverflow.com/questions/1999235/how-do-i-construct-a-cross-database-query-in-php – El Yobo Feb 21 '11 at 23:47
-
It doesn't work. It still picks up the database name from the first connection file and then says table doesn't exist.. – AAA Feb 21 '11 at 23:47
-
it is on the same server. this is the error i get: Error: Table 'database1name.databasename2.databasetable2' doesn't exist – AAA Feb 21 '11 at 23:49
-
Only one database name should be prepended. Whatever data access object you're using is probably prepending it. At this point I'm going to have to say [show me the code](http://sscce.org), which should be edited into your question. – outis Feb 21 '11 at 23:59
If it is a separate db server you might need to look into the mysql federated engine which allows you to link to databases on separate servers as if they were on the same host.

- 2,597
- 2
- 18
- 29