Jump to content

[SOLVED] How to Connect 2 database in once? and how query them?


kvlife

Recommended Posts

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");

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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;

}

}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

 

Link to comment
Share on other sites

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


 

 

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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) {

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.