View Full Version : Access 2 different mySQL DBs from one page
| OK guys, I'm a mySQL newbie and getting my feet wet so don't be too hard on me. :)
I'm working on sharing vBulletin and PHPNuke user data between the two programs, and right now I'm at baby step one. Figuring out how to access two mySQL DBs from one page.
vB uses a confusing to me DB connect routine db_site-->blah blah. I get the gist, but really don't understand the reasons behind the method. Anyway, to start small all I want to do is add a simple counter to my vB pages that store vB page info (referrer, request_uri, etc) into the PHPNuke DB.
So the question is... if vB is using db_site-->mysql_query(blah blah) How do I setup a connection to the second db?
$db2=mysql_connect($servername,$dbusername,$dbpass word);
mysql_select_db($dbname2);
And do I have to toggle the mysql_select_db each time I access the alternate DB?
I hope this makes sense. |
Is it two different databases or two different servers? If you have two different servers then you *do* need to run mysql_connect( .. ); -- however if they are two different DBs running in the same MySQL server then you can use DB_site->select_db( "bob" );
OR, you can 'cheat' by doing this:
$DB_site->query( "SELECT happy FROM joebob" );
$DB_site->query( "INSERT INTO phpnukedb.tablename VALUES( .. )" );
So you can just use the database.tablename convention to access different DBs on one page. This is usefull if you only use like 1 query to that database and the rest access the vB DB. It will save the overhead of PHP moving the DB connection and back.
| Yep it's two separate databases on two different accounts with different login/pw. :(
I couldn't sleep (thinking about this LOL) so I've been playing and did manage to fudge a working start. I used vB's PHP parsed code textbox in the styles section, and the following test code works great accross the domains.
Now I'm working on figuring out how to bury the login data in the vB config file for security reasons. At first I thought it isn't that big of a deal the data is stored inside the DB, not in a text file, but then if I screw up later and give a moderator access to the templates I would regret not having done it right to begin with.
function log_visitors() {
$ip = getenv("REMOTE_ADDR");
$referer = getenv("HTTP_REFERER");
$requested_page = getenv("REQUEST_URI");
$username = "$ip";
$guest = 1;
$n_dbhost = "myhostaddress";
$n_dbuname = "notagoodidea";
$n_dbpass = "Idontthinkso";
$n_dbname = "guesswhat";
mysql_connect($n_dbhost,$n_dbuname,$n_dbpass);
mysql_select_db("$n_dbname")
or die ("Unable to select database");
$past = time()-900;
mysql_query("DELETE FROM mpn_session
WHERE time < $past")
or die ("Unable to QUERY");
$result = mysql_query("
SELECT time
FROM mpn_session
WHERE username='$username'");
$ctime = time();
if ($row = mysql_fetch_array($result)) {
$pageviews = ($pageviews + 1);
mysql_query("
UPDATE mpn_session
SET username='$username', time='$ctime',
host_addr='$ip', guest='$guest',
pageviews =(pageviews) + 1,
requested_page='$requested_page',
refer_url='$referer'
WHERE username='$username'")
or die ("Unable to UPDATE");
} else {
mysql_query("
INSERT INTO mpn_session
(username, time, host_addr, guest,
forum_pass, pageviews, requested_page, refer_url)
VALUES ('$username', '$ctime', '$ip', '$guest','',
1, '$requested_page', '$referer')")
or die ("Unable to INSERT");
}
// echo "<p>$referer | $requested_page</p>";
}
I have a feeling I'm going about this all wrong, and that I should really dig into how vB uses sessions and just convert PHPNuke to work like vB. BUT, I think (*know*) that idea is a bit out of my league at this time. I'm having fun learning though.
Thanks for the help Mark |
vBulletin v3.5.3, Copyright ©2000-2008, Jelsoft Enterprises Ltd.