...

View Full Version : Using two databases in the same page



greens85
10-13-2009, 02:21 PM
Hi all,

I need to 'drag out' different data for the same page, i.e. some data need to come from one db and some other from a different one...

I tried to use two include statements like so:


<?php include ("db1_connect.php");?>
<?php include ("db2_connect.php");?>

but the db's seemed to conflict and no data from either would display, is there another way to do this?

Many thanks,

Greens85

CFMaBiSmAd
10-13-2009, 02:28 PM
You need to specify the correct connection to use in each query. How else would the code know which one to use?

mlseim
10-13-2009, 02:31 PM
Open one of the db's.
Query results go into an array.
Close it.

Open the other db.
Query and process with the other array.
Close it.

If possible or necessary, cache the results into a file (like an XML file),
so the process above does not need to be executed too many times.
Tell us more about what you're doing.

greens85
10-13-2009, 05:44 PM
Tell us more about what you're doing.

Basically I am pulling content from one database but we also run a forum (located on a separate database) & I want to display the last post made on the forum on the website... so one database would provide content while the other would provide the latest post from the forum.

Hope that makes sense!

mlseim
10-13-2009, 08:51 PM
OK, an array would work, but also ...
Are there any RSS feeds created that would have the last post?
Perhaps your forum creates an RSS feed (an XML) file that you can utilize.
The only drawback may be that the RSS feed might be a "short version" of the post.

But otherwise, put the first database query into an array and you can find the last post.

greens85
10-14-2009, 11:12 AM
OK, an array would work, but also ...
Are there any RSS feeds created that would have the last post?
Perhaps your forum creates an RSS feed (an XML) file that you can utilize.
The only drawback may be that the RSS feed might be a "short version" of the post.

But otherwise, put the first database query into an array and you can find the last post.

Sorry unfortunately I'm not getting this...

I'm currently opening the connection to the first database at the very top of the document, then data is pulled out at various places in the page (the last piece being at the footer).

The problem being that I cant close the connection at any time (until after the footer) but I want to display the forum posts before the footer, so when I try this it seems the databases clash!

Essentially, I can get the page to display data from one db OR the other... the only solution I can see would be to open and close a connection after every bit of data, but surely this would be a massive strain on the db and also would mean not using an include for the db credentials (which may be a security risk?)

If it helps the page in question is:

www.educationvacancies.com/jobseekers

You'll notice that near the bottom there is a part that says our online forum.

mlseim
10-14-2009, 12:31 PM
I'm saying, before you even open the main page db, open the forum db and
query the last post, saving it in an array. Close it ...

Then, open your main page db and display everything as you normally do.
When you get to the the footer, you have the array all ready to be used.
Use the array contents to display the last forum post.

greens85
10-14-2009, 12:39 PM
I'm saying, before you even open the main page db, open the forum db and
query the last post, saving it in an array. Close it ...

Then, open your main page db and display everything as you normally do.
When you get to the the footer, you have the array all ready to be used.
Use the array contents to display the last forum post.

Hey thanks,

Finally got it, sorry for being so slow! PHP is a new quantity to me:

For any other newbies experiencing the same problems, the final code was:


<?php
// connection to the forum database
$connection = mysql_connect("host", "user", "pass") or die (mysql_error());
$db = mysql_select_db("databasename", $connection) or die (mysql_error());

// post query
$query = "SELECT * FROM table ORDER BY post_id DESC LIMIT 1";
$result = mysql_query($query) or die (mysql_error());
$row = mysql_fetch_array($result);

mysql_close();

// connection to main database
include ("connect.php");
?>

And in the main content:


<?php
// show the last post from the forum
echo $row['post_subject'];
?>

mlseim
10-14-2009, 01:20 PM
There you go ... good job!
Thanks for sharing the solution ... others might find this thread useful.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum