Forum Moderators: coopster

Message Too Old, No Replies

Odd problem

Connecting to two databases

         

omoutop

8:30 am on Sep 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hi to all!

I have two database servers:

1. phpMyAdmin 2.4.0 and MySQL 3.23.58
2. phpMyAdmin 2.6.1-rc1 and MySQL 4.0.23

Both of them have a table called 'reservations' which I need to query lots of times to produce some results (I am creating some filters...anyway). I am trying to connect to the database as ussual, using a seperate script looking like this:

<?@ $db_conn = mysql_connect('localhost', 'usrname', 'password');mysql_select_db('reservations', $db_conn);?>

I have one problem and one question...

1. By using this code for connecting I can only connect to the 2nd server (2. phpMyAdmin 2.6.1-rc1 and MySQL 4.0.23)..the older server produces error messages...do I need to change my syntax to connect?

2. If I am finally able to connect to both server...can I query them both using a single query?
(both of them have table called reservations)...
example query:

$query50 = "select count(idc) as idc from reservations";
$result50 = mysql_query($query50);
$res=mysql_fetch_assoc($result50);
$resID = $res['idc'];

echo $resID;

..so if I am connected to both databases and try to count reservations table from both will it work?

any ideas would be appreciated...

thx in advance..
omoutop

Blackie

9:01 am on Sep 9, 2005 (gmt 0)

10+ Year Member



When connecting to two DBs, you should also have two DB vars

$db_conn1 = mysql_connect('localhost', 'usrname', 'password');mysql_select_db('DATABASE1', $db_conn1);

$db_conn2 = mysql_connect('localhost', 'usrname', 'password');mysql_select_db('DATABASE2', $db_conn2);

and when you send a query you state what connection to use:

This one makes a query to the first DB
$result50 = mysql_query($query50, $db_conn1);
While this one to second
$result50 = mysql_query($query50, $db_conn2);

Hope this helps

omoutop

9:11 am on Sep 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thx for the tips...I used this code to connect:

<?
$db_conn1 = mysql_connect('localhost', 'username1', 'pass1');mysql_select_db('reservations', $db_conn1);
$db_conn2 = mysql_connect('#*$!.xxx.xx.xx', 'username2', 'pass2');mysql_select_db('reservations', $db_conn2);
?>

In the browser I get:

//////////////////////////////////////
Warning: mysql_connect(): Lost connection to MySQL server during query in /home/sites/site254/web/nikos/reservations/dbconnect.php on line 3

Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in /home/sites/site254/web/nikos/reservations/dbconnect.php on line 3
Total records in Database are : 90910

Total unique Email Addresses are : 41209
////////////////////////////////////////

which means that I can not connect to the oldest database...I might need another way (syntax) to connect to the (phpMyAdmin 2.4.0 and MySQL 3.23.58) database...

omoutop

9:12 am on Sep 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



in plain words I cant connect to one of them....before querying results...

Blackie

10:08 am on Sep 9, 2005 (gmt 0)

10+ Year Member



Can you modify this line
$db_conn1 = mysql_connect('localhost', 'username1', 'pass1');
like this
$db_conn1 = mysql_connect('localhost', 'username1', 'pass1') or die('Can\'t connect to database');

to see if it's problem with establishing a connection or with a query.

I also noticed you said

Both of them have a table called 'reservations'
.
If this is the case then what is the database name?
This line
mysql_select_db('reservations', $db_conn);
should choose a database by its name not the table

omoutop

10:39 am on Sep 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



thx again...

i will try ur version $db_conn1 = mysql_connect('localhost', 'username1', 'pass1') or die('Can\'t connect to database'); but it is definately a problem with the database connection....

...ill try to find wht it is and i'll post again..
thx again!