Forum Moderators: open

Message Too Old, No Replies

Using Two Databases on One Web Page

Not Getting Info From Right Databases

         

jimh009

1:26 pm on Oct 29, 2006 (gmt 0)

10+ Year Member



Hello,

OK. Stupid MySQL question coming up.

On my site I've been using one database to store the basic information and ads (called "stuff" for this example) and all works well. I have a mysql connect with all the info, call the scripts within the page, then at the footer have the mysql close statement.

So...I decided to add a comments script to the web page. I used a second database to do this (called comments for this example). And here is where I get lost.

Everything actually works, UNLESS I try to call information or ads below the code for the comments script that uses the comments database. For example:

On the top of the page, I have multiple scripts that pull info from the "stuff" database. All works.

Near the bottom of the page, I run the comments script which pulls info from the "comments" database. This works too.

But....if I now add any more scripts BELOW the "comments" database in the code that uses the "stuff" database, I get an error because these scripts seem to be calling the "comments" database instead of the "stuff" database.

I'm kind of at a loss to what is going on, admittedly. The script I'm using that calls the "stuff" database works just fine if I insert it ABOVE the code for the "comments" database within the web page. But..when I moved it below the "comments" database within the code, I get an error because it is attempting to retrieve the information from the "comments" database, instead of the "stuff" database.

Does this make sense at all? And if it does, anyone have an idea on how to fix it?

Jim

txbakers

6:12 pm on Oct 29, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You have to make sure you use the correct connect script for the connect database.

It's not an issue, I will sometimes connect to three databases at once, you just have to make sure that you change the connect string for the proper database.

jimh009

7:12 pm on Oct 29, 2006 (gmt 0)

10+ Year Member



Hi Txbakers,

So what you are saying is...I need to use the same MySQL Connect statement on the bottom of the page for the "stuff" database that I initially used on the top of the page to connect to the "stuff" database?

I thought this might have been it, but, I didn't want to have multiple MySQL connect statements on a given page for the same database, as I thought this was not "good practice." But I'm a newbie to MySQL, so what do I know. :)

Jim

Frank_Rizzo

9:20 pm on Oct 29, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can either use handles or probably easier for you is the mysql_select_db function in php.

Examples as from php.net

handles example


$dbh1 = mysql_pconnect($host,$user,$pass);
$dbh2 = mysql_pconnect($host,$user,$pass);

You could do this...

mysql_query("USE database1",$dbh1);
mysql_query("Use database2",$dbh2);

This does the same thing as the mysql_select_db() function...

or this...

You don't even have to select the database for each connection.

mysql_query("SELECT * FROM database1.table",$dbh1);
mysql_query("SELECT * FROM database2.table",$dbh2);

mysql_select_db example

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Not connected : ' . mysql_error());
}

// make foo the current db
$db_selected = mysql_select_db('foo', $link);
if (!$db_selected) {
die ('Can\'t use foo : ' . mysql_error());
}

[edited by: Frank_Rizzo at 9:25 pm (utc) on Oct. 29, 2006]