Link to home
Start Free TrialLog in
Avatar of rowtc2
rowtc2

asked on

How to connect to 2 mysql databases from one page?

I have 2 sites hosted on 2 different servers.
site1 on server1
site2 on server2

On ftp site2 I have 2 pages: one named db.php with connection at mysql and one page with php and html code Using this code,I need to connect to database on site1 and to extract from site1 database some NAMES FROM DATABASE 1 and to put on site2.
I need help with some line codes,because i am a beginner in php. I have attached the code from the site.
<?php
$dbhost = "localhost";
$dbname = "dbnamesite1";
$dbuser = "dbusernamesite1";
$dbpasswd = "passworddbsite1";
$prefix = "";
if(!$db = @mysql_connect("$dbhost", "$dbuser", "$dbpasswd"))
die('<font size=+1>An Error Occurred</font><hr>Unable to connect to the database. <BR>Check $dbhost, $dbuser, and $dbpasswd in config.php.');
if(!@mysql_select_db("$dbname",$db))
die("<font size=+1>An Error Occurred</font><hr>Unable to find the database <b>$dbname</b> on your MySQL server.");
 
?> 
 
<?php include ("db.php");?>
<html>
<head>
<TITLE> New Document </TITLE>
<body>
 
<?php
$sql = "select * from products WHERE (id='$id') ORDER BY id DESC LIMIT 10";
$result = mysql_query ($sql);
while($row = mysql_fetch_array($result)){
 
$id=$row["id"];
$description = $row["description"];
 
echo"
 
$description
";
}
?>
 
NAMES FROM DATABASE 1
 
</body>
</html>

Open in new window

Avatar of Roger Baklund
Roger Baklund
Flag of Norway image

Connecting to two databases on two different servers at the same time is a bit tricky, but it is doable.

First you must make sure you can connect to the remote database (site1) from the server you are working on (site2). You can do that with this snippet:

<?php
$remote = mysql_connect('domain_site1.com','username','password') or die(mysql_error());
if(!mysql_select_db('database_name')) die('Could not select database');
echo 'Connected to remote server!';
?>

Replace the strings 'domain_site1.com', 'username', 'password' and 'database_name' with the appropriate values. Save it as remotedb.php and run it in the browser.

If it outputs "Connected to remote server!", the server is setup for remote connections, and we can continue with your task.

If it outputs "Unknown MySQL server host" it means the host name is wrong. Check your spelling.

If it takes a while (30 seconds) and outputs "Can't connect to MySQL server ..." it means the server does not accept remote connections. Contact the db admin on the remote site and make them change the configuration.

If it outputs "Access denied for user ..." it means the server is available for remote connections, but your username/password is wrong OR the user account is not configured for remote access. Check your spelling. If it is correct, contact the db admin on the remote site to change the configuration.

If it outputs "Could not select database" it means you are connected, but the name of the database is wrong. Check your spelling.

Start with this and report back when it works or if you need assistance.
$dbhost = "localhost";
$dbname = "dbnamesite1";
$dbuser = "dbusernamesite1";
$dbpasswd = "passworddbsite1";
$prefix = "";
if(!$db = @mysql_connect("$dbhost", "$dbuser", "$dbpasswd"))
die('<font size=+1>An Error Occurred</font><hr>Unable to connect to the database. <BR>Check $dbhost, $dbuser, and $dbpasswd in config.php.');
if(!@mysql_select_db("$dbname",$db))




$dbhost = "localhost";
$dbname1 = "dbnamesite2";
$dbuser1 = "dbusernamesite2";
$dbpasswd1 = "passworddbsite2";
$prefix = "";
if(!$db = @mysql_connect("$dbhost", "$dbuser1", "$dbpasswd1"))
die('<font size=+1>An Error Occurred</font><hr>Unable to connect to the database. <BR>Check $dbhost, $dbuser1, and $dbpasswd1 in config.php.');
if(!@mysql_select_db("$dbname1",$db))
Avatar of mmarth
mmarth

<?PHP

#####     database settings     #####     #####

#  db1
define ('gnvUSERNAME','dbusernamesite1');
define ('gnvPASSWORD','r$passworddbsite1');
define ('gnvSERVER','localhost');
define ('gnvDATABASE','dbnamesite1');

#  db2
define ('atUSERNAME','dbusernamesite2');
define ('atPASSWORD','passworddbsite2');
define ('atSERVER','localhost');
define ('atDATABASE','dbnamesite2');

#####   dateabase connect functions   #####

function openRSdb1($query)   # globalnetvantage db
{
      @mysql_connect(gnvSERVER,gnvUSERNAME,gnvPASSWORD)  or die();
      @mysql_select_db(gnvDATABASE)  or die();
      $result=@mysql_query($query)  or die();
      return $result;
}
function openRSdb2($query)   # ad-tracker db
{
      @mysql_connect(atSERVER,atUSERNAME,atPASSWORD) or die();
      @mysql_select_db(atDATABASE)  or die();
      $result=@mysql_query($query)  or die();
      return $result;
}

# use something like this to access the one db
$query = "select * from products WHERE (id='$id') ORDER BY id DESC LIMIT 10";
$sql=openRSdb1($query);
$result = mysql_query ($sql);
while($row = mysql_fetch_array($result,MYSQL_ASSOC))
{
      $description = $row["description"];
   echo"   $description";
}

# use something like this to access the other db
$rs=openRSdb2($query2);
?>
Avatar of rowtc2

ASKER

cxr , i have connected  to server using your advice . Message Connected to remote server!
What i should do next ?
You said you would extract some names from the remote site. Do you want to insert them into the local database, or just display them on the page? If you wish to insert them in a local table, do you have the same names table on the local server as the one that exists on the remote server? Or is it different tables? Do you need help creating a names table on the local server? What is the name of the table on the remote server, and which columns are in this table? Do you need all columns, or just some of them? Do you want all names records from the remote server, or only some of them?
Avatar of rowtc2

ASKER

On server 1 i have in cpanel->mysql->phpmyadmin one table named ARTICLES1 with 3 columns
id    name     surname
1    Arthur    John
2    Mariah    Carey

On server 2 i need to connect to this table on server 1 and to show some names. I have php code to make the association with the right id to know to extract  the name based on a  specific id. I dont want to insert in a local table ,i want  just to show on web page.

I  dont know how to connect succesfully on two databases from one page and to show informations from 2 databases .See this code from my site. How do i show informations from server1 after
 <!-- NAMES FROM DATABASE 1 --> ?  (i need exactly some lines in code i have attached who will work). Just this page as it is.



<?php
$dbhost = "localhost";
$dbname = "dbnamesite1";
$dbuser = "dbusernamesite1";
$dbpasswd = "passworddbsite1";
$prefix = "";
if(!$db = @mysql_connect("$dbhost", "$dbuser", "$dbpasswd"))
die('<font size=+1>An Error Occurred</font><hr>Unable to connect to the database. <BR>Check $dbhost, $dbuser, and $dbpasswd in config.php.');
if(!@mysql_select_db("$dbname",$db))
die("<font size=+1>An Error Occurred</font><hr>Unable to find the database <b>$dbname</b> on your MySQL server.");
 
?> 
 
<?php include ("db.php");?>
<html>
<head>
<TITLE> New Document </TITLE>
<body>
 
<?php
$sql = "select * from products WHERE (id='$id') ORDER BY id DESC LIMIT 10";
$result = mysql_query ($sql);
while($row = mysql_fetch_array($result)){
 
$id=$row["id"];
$description = $row["description"];
 
echo"
 
$description
";
}
?>
 
<!-- NAMES FROM DATABASE 1 -->
 
<?php
$sql = "select * from ARTICLES1 WHERE (id='$id') ORDER BY id DESC LIMIT 10";
$result = mysql_query ($sql);
while($row = mysql_fetch_array($result)){
 
$id=$row["id"];
$name= $row["name"];
 
echo"
$name 
 
 
$description
";
}
?>
 
</body>
</html>

Open in new window

In the file you created before named "remotedb.php", remove the last line (echo ...).

I assume the first part of your snippet is the content of db.php?

Try the code below, the only changes are including the remotedb.php and in the mysql_query() function calls, you must include the link id from the corresponding connect command:

$result = mysql_query ($sql,$db);
$result = mysql_query ($sql,$remote);
<?php 
  include("db.php");
  include("remotedb.php");
?>
<html>
<head>
<TITLE> New Document </TITLE>
<body>
 
<?php
$sql = "select * from products WHERE (id='$id') ORDER BY id DESC LIMIT 10";
$result = mysql_query ($sql,$db);
while($row = mysql_fetch_array($result)){
 
$id=$row["id"];
$description = $row["description"];
 
echo"
 
$description
";
}
?>
 
<!-- NAMES FROM DATABASE 1 -->
 
<?php
$sql = "select * from ARTICLES1 WHERE (id='$id') ORDER BY id DESC LIMIT 10";
$result = mysql_query ($sql,$remote);
while($row = mysql_fetch_array($result)){
 
$id=$row["id"];
$name= $row["name"];
 
echo"
$name 
 
 
$description
";
}
?>
 
</body>
</html>

Open in new window

...I suppose it should be

echo "$id  $name<br />";

There is no $description in the second query, and the linefeeds will not show on a html page, you need <br /> to break the lines.
Avatar of rowtc2

ASKER

I am testing now and looks to be ok.
I have a question.

Why you put instead remotedb , just remote(and why is working in this way)?
$result = mysql_query ($sql,$db);
$result = mysql_query ($sql,$remote);

If the files from ftp are named db2.php  and remotedb.php will be normal to use
$result = mysql_query ($sql,$db2);
$result = mysql_query ($sql,$remotedb);

Now the files names on ftp are db2.php and remotedb.php
ASKER CERTIFIED SOLUTION
Avatar of Roger Baklund
Roger Baklund
Flag of Norway image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rowtc2

ASKER

Thank you very much sir.