简体   繁体   中英

Inner Join query on 2 different databases - Mysql

We have 2 different databases that I'm trying query against each other with an inner join.

When I run the query from phpmyadmin, the query works perfectly. However, when I attempt to put the query into a php page, I cannot get the line to work. I assume I'm missing something in the mysql_select_db line where I reference the host/db/user/pass for the first database.

What am I missing here to get this query to function on the page? Again, I'm confident the actual query works since it does run in phpmyadmin.

Thanks in advance as always.

Here's the code I'm working with....

$hostname_db = "123.456.78.910";
$database_db = "votes_db";
$username_db = "votes_dbuser";
$password_db = "password123";
$db = mysql_connect($hostname_db, $username_db, $password_db, true) or trigger_error(mysql_error(),E_USER_ERROR); 

$hostname_db2 = "123.456.78.910";
$database_db2 = "survey_db";
$username_db2 = "survey_dbuser";
$password_db2 = "password456";
$db2 = mysql_connect($hostname_db2, $username_db2, $password_db2, true); 

// trying to make this work, query ok in phpmyadmin, but not on the php page
mysql_select_db($database_db, $db);
$query_testdb3 = sprintf("SELECT votes_db.vote_table.vote_survey_id
FROM votes_db.vote_table 
inner join survey_db.survey_table 
ON votes_db.vote_table.vote_survey_id = survey_db.survey_table.survey_id
WHERE votes_db.vote_table.vote_survey_id = 1457 ");
$testdb3 = mysql_query($query_testdb3, $db) or die(mysql_error());
$row_testdb3 = mysql_fetch_assoc($testdb3);
$totalRows_testdb3 = mysql_num_rows($testdb3);

Sometime ago I have the same issue ( mysql-php multiple databases problem )

So, remove the "true" option on the second ($db2) and everything should be fine.

Gl

$`totalRows_testdb3 = mysql_num_rows($testdb3);`

change the column # in "$testdb3" in the last line to "$row_testdb3".

$totalRows_testdb3 = mysql_num_rows($row_testdb3);

Also if you put each $db and db2 in separate class and function with a:

mysql_close($db);

after each query.

It was a permissions problem. The user that was connecting to the database didn't have the proper privliges. Hosting company fixed this for me, so I can't tell you exactly what they did, but it now works.

Tim was correct, it was working from phpmyadmin since I was logged in as a superuser...

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