Jump to content

[SOLVED] Multiple Database Connections


MasterACE14

Recommended Posts

with my ads script, it doesn't work when I use it on my websites that connect to a different database.

 

Like this...

connect to database

 

page content

 

ads script (connects to ads database)

 

rest of page content

 

here's the ads connection script:

<?php

$conAdscon = mysql_connect("localhost",$mysql['user'],$mysql['pass']) or die(mysql_error());
$conAds = mysql_select_db("eliteace_client",$conAdscon);
$AdsQuery = mysql_query("SELECT * FROM `c_ads` WHERE `active`='1'") or die(mysql_error());

while($AdsArr = mysql_fetch_array($AdsQuery)) 
{
$ad[] = array($AdsArr['title'], $AdsArr['description'], $AdsArr['displayurl'], $AdsArr['url']);
}

mysql_close($conAdscon);


?>

 

any suggestions?  :-\

 

Regards, ACE

Link to comment
https://forums.phpfreaks.com/topic/153419-solved-multiple-database-connections/
Share on other sites

Define: doesn't work. What does it do or not do, what are the symptoms, errors, results?

 

If the two databases are on the same database server, with the same hostname, user, and password, there is no need to create a new connection and in fact your code would be reusing the existing connection and the mysql_close() statement would be closing the connection that the rest of the page needs.

the connection details are the same, it's just the database that is different.

$conAdscon = mysql_connect("localhost",$mysql['user'],$mysql['pass']) or die(mysql_error());
$conAds = mysql_select_db("eliteace_client",$conAdscon); // the website this ad script is going in uses eliteace_realmbattlesdev database

If a second call is made to mysql_connect() with the same arguments, no new link will be established, but instead, the link identifier of the already opened link will be returned.

 

Your mysql_close() statement is closing the single link. Just select the correct database before the query and set it back to the main database after the query or specify the database in the query.

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.