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.

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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

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.