Jump to content

Can include be used to call multiple databases ?


swatisonee

Recommended Posts

Hi,

 

I have my admin data (users, contact info etc) in DB1. DB2 and DB3 contains work info and needs to only pull in user info from DB1 and then send back other info into DB1. Note each DB has it own set of tables.  The reason for this is that all Users are in DB1. Some of them have access to data in DB2, others to that in DB3. However, some data from DB2 and DB3 must get populated into DB1

 

My query is :

 

a. I have opened DB2.

b. I need to copy data from DB2 to DB1 (ie. say user info of UserA in Table1 of DB2 should also be reflected TableX of DB1) .

For purpose of simplicity i have used birthdate and username but the data is acutally of a different nature.

 

I thought i could use include as under but i have no access to DB1 at all. Would appreciate if someone could throw light on this / Thanks


<?
//this is DB2
header("Cache-Control: public");
include ("../include/session.php"); // session start
include ("../indl.php"); //db info for DB2
$userid = $_SESSION['userid']; // works fine
?>

<?
if(!isset($_SESSION['userid'])){
echo "<center><font face='Calibri' size='2' color=red>Sorry, Please login and use this page </font></center>";
exit;}

echo $userid; 
//works;

?>

<?

include ("../db1.php"); //db info for DB1 

//db1.php is as under 
// <?
// error_reporting(E_ERROR | E_PARSE | E_CORE_ERROR);
// 
// $dbservertype='mysql';
// $servername='localhost';
// $dbusername1='abc';
// $dbpassword1='xyz';
// $dbname1='123';
// connecttodb($servername,$dbname1,$dbusername1,$dbpassword1);
// function connecttodb($servername,$dbname1,$dbusername1,$dbpassword1)
// {
// global $link1;
// $link1=mysql_connect ("$servername","$dbusername1","$dbpassword1", true);
// if(!$link){die("Could not connect to MySQL");}
// mysql_select_db("$dbname1",$link1) or die ("could not open db".mysql_error());
// }
// 
// echo $link1;
// ?>

$query = "select * from `123.users`" ; 
// heres the problem - does mysql know that i'm calling Table users from DB1?
$whichdb = $dbname1; //IE DB1 which is named `123`
$result = mysql_query($query,$whichdb) or die(mysql_error());    
$row = mysql_fetch_array($result);
$bd= $row1[birthdate];
$un= $row1[username];

$query2 = "INSERT INTO `zeetest` (`B` , `C`) VALUES ('$bd', '$un')"; // this table is in DB2
$whichdb = $dbname2; //ie DB2  
$result2 = mysql_query($query2,$whichdb) or die(mysql_error());    

?> 

 

 

 

 

 

 

 

 

 

Link to comment
Share on other sites

Thanks for the reply . I thought  that's what i did when i used

$link1=mysql_connect ("$servername","$dbusername1","$dbpassword1", true);

in the file db1.php and used include to bring it into DB2. 

But it still wont run.

 

Would you suggest i bring it in as under directly in  DB2 without using include ? It then means i will be have to include the DB1 user info in each script .

 


<?

$dbusername1='abc';
$dbpassword1='xyz';
$dbname1='123';
$link1=mysql_connect ("$servername","$dbusername1","$dbpassword1", true);
if(!$link1){die("Could not connect to MySQL");}
mysql_select_db("$dbname1",$link1) or die ("could not open db".mysql_error());
}


$query = "select * from `123.`users`" ; // heres the problem - does mysql know that Table users is in DB1?
//$whichdb = $dbname1; //IE DB1 which is named `123`
$result = mysql_query($query,$link1) or die(mysql_error());    
$row = mysql_fetch_array($result);
$bd= $row1[birthdate];
$un= $row1[username];

$query2 = "INSERT INTO `zeetest` (`B` , `C`) VALUES ('$bd', '$un')"; // this table is in DB2
$whichdb = $dbname2; //ie DB2 . Will mysql know that i am also calling the 2nd db ?
$result2 = mysql_query($query2,$whichdb) or die(mysql_error());    

?> 

Link to comment
Share on other sites

Would you suggest i bring it in as under directly in  DB2 without using include ? It then means i will be have to include the DB1 user info in each script .

 

Using an include or not for your connection has no effect.

 

In your code, I don't see where $whichdb is defined. Your also not checking your SELECT query actually works before trying to use its results.

Link to comment
Share on other sites

thank you thorpe.much appreciated.  it did work ! now i have struggle on my next problem - uploading csv,excel files from a user into a table :-(

include ("../db1.php"); //db info for DB1 
$query = "select * from `users`" ; // heres the problem - does mysql know that Table users is in DB1?
$which = $link1;  
$result = mysql_query($query,$which) or die(mysql_error());    
while($row = mysql_fetch_array($result))
{ 
$sid= $row[staffid];
$un= $row[username];     

$query2 = "INSERT INTO `zeetest` (`B`,`C`) VALUES ('$sid','$un')"; // this table is in DB2
$result2 = mysql_query($query2,$link) or die(mysql_error());    
//$link is in DB2 and appears in the first include at the very top of the wholepage and contains DB2 un,pw
}
?>

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.