Jump to content

[SOLVED] A quick question/answer


dhimok

Recommended Posts

Ok. Here s an example how i connect to one of them

 


$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'pass';
$dbname = 'database1';


$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);

// here s the website

mysql_close($conn);

 

And I want to connect to the other database then .... do I select the other database ... A quick example

$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'pass';
$dbname = 'database1';

$dbname2 = 'database2';
$dbpass2 = 'dbpass2';
$dbuser2= 'dbuser2';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
$conn2 = mysql_connect($dbhost, $dbuser2, $dbpass2) or die ('Error connecting to mysql');
mysql_select_db($dbname, $conn);
mysql_select_db($dbname2, $conn2);

// here s the website
mysql_query($query, $conn);
mysql_query($query, $conn2);
mysql_close($conn);

Yes you can. You'll need to setup a link identifier when you connect to the mysql servers. Then when you use any mysql function that requires a link identifier (second parameter for function) you must provide it, Heres an example of how to use the link identifier:

<?php

// connect to mysql server 1
// $server1 variable is the link identifier for sending queries to server1
$server1 = mysql_connect('server1.mysite.com', 'server1username', 'server1password');

// connect to mysql server 2
// $server2 variable is the link identifier for sending queries to server2
$server2 = mysql_connect('server2.mysite.com', 'server2username', 'server2password');

// select database for server1:
mysql_select_db('server1database', $server1);

// select database for server2:
mysql_select_db('server2database', $server2);

// NOTE: We have used the link identifier above in the second parameter for mysql_select_db function. We do this for all mysql_* functions that accept link identifiers. here is a few more

// query server1:
$qry1 = mysql_query("SELECT * FROM table", $server1);

// get results from query1:
$rows = mysql_fetch_array($qry1);

// query server2:
$qry = mysql_query("SELECT * FROM table", $server2);

// get results from query2:
$rows = mysql_fetch_array($qry2);

// NOTE: We don't provide a link identifier for mysql_fetch_* functions. They only require the resource identifier which passed down from a call to mysql_query.

// close connection to servers:
mysql_close($server1);
mysql_close($server2);

?>

 

EDIT: Frost beat me  ;D

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.