jeff5656 Posted July 15, 2011 Share Posted July 15, 2011 I trying to join two tables in 2 separate databases, mainbill and demo. $query2 = "SELECT * FROM mainbill.notes inner join mainbill.notes ON mainbill.notes.id = demo.notes_list.bill_id"; When I run this query I get: Not unique table/alias: notes. Now I do not have a notes table in the demo database so I can't figure out what the problem is. Both databases are on the same server. Quote Link to comment https://forums.phpfreaks.com/topic/242080-problem-with-querying-from-2-databases/ Share on other sites More sharing options...
AyKay47 Posted July 15, 2011 Share Posted July 15, 2011 mainbill.notes is a table reference to the table notes...that's what you are telling it, looks like you are joining the same table...which makes no sense Quote Link to comment https://forums.phpfreaks.com/topic/242080-problem-with-querying-from-2-databases/#findComment-1243201 Share on other sites More sharing options...
jeff5656 Posted July 15, 2011 Author Share Posted July 15, 2011 I'm trying to join the table notes (in the mainbill database) with the table notes_list (in the demo database). the join is via id (in the notes table) with bill_id (in the notes_id table). Quote Link to comment https://forums.phpfreaks.com/topic/242080-problem-with-querying-from-2-databases/#findComment-1243207 Share on other sites More sharing options...
AyKay47 Posted July 15, 2011 Share Posted July 15, 2011 $query2 = "SELECT * FROM mainbill.notes inner join demo.notes_list ON mainbill.notes.id = demo.notes_list.bill_id"; Quote Link to comment https://forums.phpfreaks.com/topic/242080-problem-with-querying-from-2-databases/#findComment-1243212 Share on other sites More sharing options...
jeff5656 Posted July 15, 2011 Author Share Posted July 15, 2011 Thanks - now I get a new error: SELECT command denied to user 'user_nameofdb'@'localhost' for table 'notes' So my question now is, how do I connect to 2 databases in order to run this query? I think that's the problem... Quote Link to comment https://forums.phpfreaks.com/topic/242080-problem-with-querying-from-2-databases/#findComment-1243219 Share on other sites More sharing options...
AyKay47 Posted July 15, 2011 Share Posted July 15, 2011 well to put it plainly, you can't however by indicating the database in your select query, MYSQL allows another database to be called...so that's not the issue here.. 1. Are you sure that you can execute query's with the given connection information? 2. Which database are you using in your mysql_select_db? Quote Link to comment https://forums.phpfreaks.com/topic/242080-problem-with-querying-from-2-databases/#findComment-1243222 Share on other sites More sharing options...
jeff5656 Posted July 15, 2011 Author Share Posted July 15, 2011 here's the connect code right before that query: $dbhost = "localhost"; $dbname = "xxxx_mainbill"; $dbuser = "xxxxx_yyyy"; $dbpass = "xxxxx"; mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error()); mysql_select_db($dbname) or die(mysql_error()); Importantly, the dbuser and dbpassword is the same for the other database (demo) Quote Link to comment https://forums.phpfreaks.com/topic/242080-problem-with-querying-from-2-databases/#findComment-1243224 Share on other sites More sharing options...
AyKay47 Posted July 15, 2011 Share Posted July 15, 2011 okay, try this $query2 = "SELECT * FROM notes inner join demo.notes_list ON notes.id = demo.notes_list.bill_id"; Quote Link to comment https://forums.phpfreaks.com/topic/242080-problem-with-querying-from-2-databases/#findComment-1243226 Share on other sites More sharing options...
jeff5656 Posted July 15, 2011 Author Share Posted July 15, 2011 I get the same arror except it says SELECT command denied to user 'user_nameofdb'@'localhost' for table 'notes_list' Since the username and pw are the same for both databases, I wonder why this doesn't work... Quote Link to comment https://forums.phpfreaks.com/topic/242080-problem-with-querying-from-2-databases/#findComment-1243227 Share on other sites More sharing options...
fenway Posted July 15, 2011 Share Posted July 15, 2011 Then your GRANTs are incorrect. Quote Link to comment https://forums.phpfreaks.com/topic/242080-problem-with-querying-from-2-databases/#findComment-1243232 Share on other sites More sharing options...
jeff5656 Posted July 15, 2011 Author Share Posted July 15, 2011 "Then your GRANTs are incorrect." I can connect to both databases individually (just tried it) - but when I try to connect to both simultaneously with the above query I get the error. Quote Link to comment https://forums.phpfreaks.com/topic/242080-problem-with-querying-from-2-databases/#findComment-1243238 Share on other sites More sharing options...
jeff5656 Posted July 15, 2011 Author Share Posted July 15, 2011 Ok I'm sorry I think I just wasted all of your time. I was not naming the databases correctrly. for example the database is called myusername_demo NOT demo I a really sorry but at least I still needed the correct JOIN command which you guys did help me with so all is not lost! Thanks again... Quote Link to comment https://forums.phpfreaks.com/topic/242080-problem-with-querying-from-2-databases/#findComment-1243241 Share on other sites More sharing options...
AyKay47 Posted July 15, 2011 Share Posted July 15, 2011 you know what, I thought that to myself when you posted xxxx_mainbill as your db name, I dismissed it as you trying to through us off!! Anyway, glad this is sorted Quote Link to comment https://forums.phpfreaks.com/topic/242080-problem-with-querying-from-2-databases/#findComment-1243243 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.