Jump to content

[SOLVED] Connect to two different MySQL databases using two different connect files!


Dale_G

Recommended Posts

Hey everyone, basically I have two includes named 'connect.php' and 'connect2.php', they consist of the following code...

 

$username = '';
$database = '';
$password = '';
$host = '';

mysql_connect( $host, $username, $password );
@mysql_select_db( $database );

 

Of course, with the proper values filled in for the respective database. When I try to include them both in a file, one of them seems to get "ignored' and the SQL queries that need that file don't work, however one of them does. I know WHY this doesn't work, I just need to know a solution so that I can include a file and can run different queries on different databases.

use


$db1 = mysql_connect( $host, $username, $password );

 

$db1 will store link to one database, and you should of course have $db2 to store link to the other one.

 

You will have to use this variables to indicate which database to use in all mysql statements.

 

//example
mysql_query($query1,$db1);
mysql_query($query2,$db2);

 

See: http://www.php.net/manual/en/function.mysql-connect.php

 

 

Name the connection, and pass it as the second parameter to mysql_* functions:

 

<?php
$conn1 = mysql_connect('', '', '');
$conn2 = mysql_connect('', '', '');
mysql_select_db('foo', $conn1);
mysql_select_db('bar', $conn2);
$result = mysql_query("SELECT * FROM test", $conn1);
//etc

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.