kvlife Posted July 5, 2009 Share Posted July 5, 2009 somebody help please... connect 2 database is that work? and how to query them? thanks you $link_web = mysql_connect($domain_web,$username_web,$password_web); $link_web2 = mysql_connect($domain_web2,$username_web2,$password_web2); mysql_select_db($database_web,$link_web) or die("Unable Select ".$database_web." Database"); mysql_select_db($database_web2,$link_web2) or die("Unable Select ".$database_web2." Database"); Quote Link to comment https://forums.phpfreaks.com/topic/164845-solved-how-to-connect-2-database-in-once-and-how-query-them/ Share on other sites More sharing options...
.josh Posted July 5, 2009 Share Posted July 5, 2009 assign it to a variable $conn1 = mysql_connect(...); $conn2 = mysql_connect(...); Quote Link to comment https://forums.phpfreaks.com/topic/164845-solved-how-to-connect-2-database-in-once-and-how-query-them/#findComment-869217 Share on other sites More sharing options...
kvlife Posted July 5, 2009 Author Share Posted July 5, 2009 assign it to a variable $conn1 = mysql_connect(...); $conn2 = mysql_connect(...); That i try it already, but still can't work on database 2... database 1 and database 2... it is a seperate work... how can work it like that? Quote Link to comment https://forums.phpfreaks.com/topic/164845-solved-how-to-connect-2-database-in-once-and-how-query-them/#findComment-869220 Share on other sites More sharing options...
.josh Posted July 5, 2009 Share Posted July 5, 2009 ah, well you edited in what you tried after I posted. No, you cannot do something like assign mysql_select_db to a variable and use that. You can only have 1 active database selected at a time, so you can assign the connections to 2 different vars but you still need to first call mysql_select_db to specify the db each time you want to switch making a query to whichever db. Quote Link to comment https://forums.phpfreaks.com/topic/164845-solved-how-to-connect-2-database-in-once-and-how-query-them/#findComment-869223 Share on other sites More sharing options...
kvlife Posted July 5, 2009 Author Share Posted July 5, 2009 ah, well you edited in what you tried after I posted. No, you cannot do something like assign mysql_select_db to a variable and use that. You can only have 1 active database selected at a time, so you can assign the connections to 2 different vars but you still need to first call mysql_select_db to specify the db each time you want to switch making a query to whichever db. Sorry for edited my post, because i forget to put the code in the post ~.~ well, thanks your advice and i will try it Quote Link to comment https://forums.phpfreaks.com/topic/164845-solved-how-to-connect-2-database-in-once-and-how-query-them/#findComment-869226 Share on other sites More sharing options...
kvlife Posted July 5, 2009 Author Share Posted July 5, 2009 How does i use mysql_db_query() that will error? And my connection just connect in 1 database , that i want to connect into 2nd database already like that? Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in D:\xampplite\htdocs\roxinity\5-7-09\roxinity\function\func_register.php on line 13 function account($username,$password,$cpassword,$sex,$email,$ip) { $query = "SELECT * FROM account where userid='$username'"; $nums = mysql_db_query($db,$query); if(mysql_num_rows($nums) <= 0) { $query = "SELECT * FROM account where userid='$email'"; $nums = mysql_db_query($db,$query); if(mysql_num_rows($nums) <= 0) { if($password != $cpassword) { return 2; } else { $query = "INSERT INTO account ('userid','user_pass','sex','email','last_ip') values('$username','$password','$sex','$email','$ip')"; mysql_db_query($db,$query); return 3; } } else { return 1; } } else { return 0; } } Quote Link to comment https://forums.phpfreaks.com/topic/164845-solved-how-to-connect-2-database-in-once-and-how-query-them/#findComment-869237 Share on other sites More sharing options...
PFMaBiSmAd Posted July 5, 2009 Share Posted July 5, 2009 mysql_select_db is per connection. As long as you specify the correct connection identifier every where it is needed, what you are doing has a chance to work. However you are using both mysql_select_db and mysql_db_query to try to select the database and in your function $db does not have any value, so mysql_db_query is failing. mysql_db_query was depreciated a really long time ago in about the year 2000 and should not be used. Change the mysql_db_query to the correct usage of mysql_query and use the correct connection identifier and you can get it to work. Edit: and you should be developing and debugging php code on a system with error_reporting set to E_ALL so that php would help you find problems like the $db variable that did not exist. Quote Link to comment https://forums.phpfreaks.com/topic/164845-solved-how-to-connect-2-database-in-once-and-how-query-them/#findComment-869243 Share on other sites More sharing options...
kvlife Posted July 5, 2009 Author Share Posted July 5, 2009 mysql_select_db is per connection. As long as you specify the correct connection identifier every where it is needed, what you are doing has a chance to work. However you are using both mysql_select_db and mysql_db_query to try to select the database and in your function $db does not have any value, so mysql_db_query is failing. mysql_db_query was depreciated a really long time ago in about the year 2000 and should not be used. Change the mysql_db_query to the correct usage of mysql_query and use the correct connection identifier and you can get it to work. Edit: and you should be developing and debugging php code on a system with error_reporting set to E_ALL so that php would help you find problems like the $db variable that did not exist. If i use mysql_select_db and mysql_query, how does it work on both of database? Quote Link to comment https://forums.phpfreaks.com/topic/164845-solved-how-to-connect-2-database-in-once-and-how-query-them/#findComment-869247 Share on other sites More sharing options...
.josh Posted July 5, 2009 Share Posted July 5, 2009 You have to call mysql_select_db every time you want to switch dbs $link_web = mysql_connect($domain_web,$username_web,$password_web); $link_web2 = mysql_connect($domain_web2,$username_web2,$password_web2); mysql_select_db($database_web,$link_web) or die("Unable Select ".$database_web." Database"); mysql_query('query1',$link_web); // query from first db mysql_query('query2',$link_web); // query from first db (don't need to call select_db again since it's same db mysql_select_db($database_web2,$link_web2) or die("Unable Select ".$database_web2." Database"); mysql_query('query3',$link_web2); // query from 2nd db mysql_select_db($database_web,$link_web) or die("Unable Select ".$database_web." Database"); mysql_query('query4',$link_web); // query from 1st db Quote Link to comment https://forums.phpfreaks.com/topic/164845-solved-how-to-connect-2-database-in-once-and-how-query-them/#findComment-869252 Share on other sites More sharing options...
PFMaBiSmAd Posted July 5, 2009 Share Posted July 5, 2009 Using the correct link identifier in the query is enough if the database has already been selected for that link. The database that is selected is per link identifier (assuming the link id is used in the select statement.) You can also use the db_name.table_name syntax in your queries. Quote Link to comment https://forums.phpfreaks.com/topic/164845-solved-how-to-connect-2-database-in-once-and-how-query-them/#findComment-869257 Share on other sites More sharing options...
kvlife Posted July 5, 2009 Author Share Posted July 5, 2009 anyway, thanks you all... i solve it already, if any problem , i will ask again... ^^ Quote Link to comment https://forums.phpfreaks.com/topic/164845-solved-how-to-connect-2-database-in-once-and-how-query-them/#findComment-869270 Share on other sites More sharing options...
kvlife Posted July 5, 2009 Author Share Posted July 5, 2009 how can 2 db in once for select statement? Is it like that? mysql_select_db($database_web,$link_web1); mysql_select_db($database_web2,$link_web2); $query = "SELECT ".$database_web1.".title,".$database_web1.".message,".$database_web2.".userid FROM ".$database_web1.".annoucements LEFT JOIN ".$database_web1.".login ON ".$database_web1.".acid = ".$database_web2.".account_id"; $news = mysql_query($query,$link_web1); $news .= mysql_query($query,$link_web2); if(mysql_num_rows($news) > 0) { Quote Link to comment https://forums.phpfreaks.com/topic/164845-solved-how-to-connect-2-database-in-once-and-how-query-them/#findComment-869336 Share on other sites More sharing options...
PFMaBiSmAd Posted July 5, 2009 Share Posted July 5, 2009 Queries are performed over a connection to a database server. Unless both databases are accessible through a single connection, it is not possible to do what you are asking in one query. Are the two databases on separate database servers? Quote Link to comment https://forums.phpfreaks.com/topic/164845-solved-how-to-connect-2-database-in-once-and-how-query-them/#findComment-869341 Share on other sites More sharing options...
kvlife Posted July 5, 2009 Author Share Posted July 5, 2009 Queries are performed over a connection to a database server. Unless both databases are accessible through a single connection, it is not possible to do what you are asking in one query. Are the two databases on separate database servers? Ermmm ya ,the two database just separate in different database server... By the way , if they are same connection, but they aren't same database, how to combine them? Quote Link to comment https://forums.phpfreaks.com/topic/164845-solved-how-to-connect-2-database-in-once-and-how-query-them/#findComment-869342 Share on other sites More sharing options...
PFMaBiSmAd Posted July 5, 2009 Share Posted July 5, 2009 If they are on the same database server - You can also use the db_name.table_name syntax in your queries. Quote Link to comment https://forums.phpfreaks.com/topic/164845-solved-how-to-connect-2-database-in-once-and-how-query-them/#findComment-869345 Share on other sites More sharing options...
kvlife Posted July 5, 2009 Author Share Posted July 5, 2009 If they are on the same database server - You can also use the db_name.table_name syntax in your queries. can you do a example code for me to research??? thanks you... Quote Link to comment https://forums.phpfreaks.com/topic/164845-solved-how-to-connect-2-database-in-once-and-how-query-them/#findComment-869346 Share on other sites More sharing options...
PFMaBiSmAd Posted July 5, 2009 Share Posted July 5, 2009 http://mysql.he.net/doc/refman/5.1/en/identifier-qualifiers.html Quote Link to comment https://forums.phpfreaks.com/topic/164845-solved-how-to-connect-2-database-in-once-and-how-query-them/#findComment-869359 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.