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

rowtc2Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Roger BaklundCommented:
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.
0
michofreihaCommented:
$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))
0
mmarthCommented:
<?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);
?>
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

rowtc2Author Commented:
cxr , i have connected  to server using your advice . Message Connected to remote server!
What i should do next ?
0
Roger BaklundCommented:
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?
0
rowtc2Author Commented:
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

0
Roger BaklundCommented:
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

0
Roger BaklundCommented:
...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.
0
rowtc2Author Commented:
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
0
Roger BaklundCommented:
The $ sign in front of the names indicates a php variable. The php-file named remotedb.php contains this line:

$remote = mysql_connect('domain_site1.com','username','password')

This creates a variable named $remote, and this variable is used in the mysqL_query() call. The name of the php file is not relevant in this context.

If you have renamed the db.php file to db2.php, you must change the include() statement:

include("db2.php");

...but unless you also changed the variable name inside the file, the variable is still called $db.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rowtc2Author Commented:
Thank you very much sir.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.