Connecting to two databases using PHP
Posted: Fri Aug 01, 2008 6:42 am
I was trying to connect to two databases which are on the same host using the same user name and password. I wanted to establish the connnection to read the data from one of the tables in the first database and insert it in one of the tables in the second database. I wrote 2 PHP pages to connect to the two databases and included them in the file which contains the select and insert statment.But it is selecting the records from the first table and appending the results in that same table. Here is my code which includes the database connection pages.. Can u help me?
<?php
session_start();
include_once 'cls_connect_dbtest.php';
$dbtest = new connectTest_db;
include_once 'cls_connect_dbtestrms.php';
$dbtestrms = new connectTestrms_db;
//get all the data from the first table of the first database
$query = "SELECT * FROM batch ";
$result = @mysql_query($query,$dbtestrms->linktestrms) or die("Error cccc: ".mysql_error());
$nbrows_first = @mysql_num_rows($result);
while($rowdata = @mysql_fetch_array($result)){
$value1=$rowdata['batch_id'];
$value2=$rowdata['batch_code'];
$query_insert = "INSERT INTO batch(batch_id,batch_code) VALUES($value1, $value2) ";
$result_insert = @mysql_query($query_insert,$dbtest->linktest) or die("Error ddddd: ".mysql_error());
$nbrows = @mysql_num_rows($result_insert);
} //while($rowdata = @mysql_fetch_array($result))
print"Number of rows in the test table:$nbrows<hr>";
print "Query executed successfully!!<hr>";
?>
<?php
session_start();
include_once 'cls_connect_dbtest.php';
$dbtest = new connectTest_db;
include_once 'cls_connect_dbtestrms.php';
$dbtestrms = new connectTestrms_db;
//get all the data from the first table of the first database
$query = "SELECT * FROM batch ";
$result = @mysql_query($query,$dbtestrms->linktestrms) or die("Error cccc: ".mysql_error());
$nbrows_first = @mysql_num_rows($result);
while($rowdata = @mysql_fetch_array($result)){
$value1=$rowdata['batch_id'];
$value2=$rowdata['batch_code'];
$query_insert = "INSERT INTO batch(batch_id,batch_code) VALUES($value1, $value2) ";
$result_insert = @mysql_query($query_insert,$dbtest->linktest) or die("Error ddddd: ".mysql_error());
$nbrows = @mysql_num_rows($result_insert);
} //while($rowdata = @mysql_fetch_array($result))
print"Number of rows in the test table:$nbrows<hr>";
print "Query executed successfully!!<hr>";
?>