Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-13-2009, 02:21 PM   PM User | #1
greens85
Regular Coder

 
Join Date: Sep 2007
Posts: 809
Thanks: 5
Thanked 2 Times in 2 Posts
greens85 is an unknown quantity at this point
Using two databases in the same page

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 Code:
<?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
greens85 is offline   Reply With Quote
Old 10-13-2009, 02:28 PM   PM User | #2
CFMaBiSmAd
Senior Coder

 
CFMaBiSmAd's Avatar
 
Join Date: Oct 2006
Location: Denver, Colorado USA
Posts: 2,744
Thanks: 2
Thanked 256 Times in 248 Posts
CFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the rough
You need to specify the correct connection to use in each query. How else would the code know which one to use?
__________________
If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.
CFMaBiSmAd is offline   Reply With Quote
Old 10-13-2009, 02:31 PM   PM User | #3
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,055
Thanks: 8
Thanked 1,032 Times in 1,023 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
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.
mlseim is offline   Reply With Quote
Old 10-13-2009, 05:44 PM   PM User | #4
greens85
Regular Coder

 
Join Date: Sep 2007
Posts: 809
Thanks: 5
Thanked 2 Times in 2 Posts
greens85 is an unknown quantity at this point
Quote:
Originally Posted by mlseim View Post
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!
greens85 is offline   Reply With Quote
Old 10-13-2009, 08:51 PM   PM User | #5
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,055
Thanks: 8
Thanked 1,032 Times in 1,023 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
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.
mlseim is offline   Reply With Quote
Old 10-14-2009, 11:12 AM   PM User | #6
greens85
Regular Coder

 
Join Date: Sep 2007
Posts: 809
Thanks: 5
Thanked 2 Times in 2 Posts
greens85 is an unknown quantity at this point
Quote:
Originally Posted by mlseim View Post
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.

Last edited by greens85; 10-14-2009 at 11:24 AM..
greens85 is offline   Reply With Quote
Old 10-14-2009, 12:31 PM   PM User | #7
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,055
Thanks: 8
Thanked 1,032 Times in 1,023 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
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.
mlseim is offline   Reply With Quote
Old 10-14-2009, 12:39 PM   PM User | #8
greens85
Regular Coder

 
Join Date: Sep 2007
Posts: 809
Thanks: 5
Thanked 2 Times in 2 Posts
greens85 is an unknown quantity at this point
Quote:
Originally Posted by mlseim View Post
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 Code:
<?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 Code:
<?php
// show the last post from the forum
    
echo $row['post_subject'];
?>
greens85 is offline   Reply With Quote
Old 10-14-2009, 01:20 PM   PM User | #9
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,055
Thanks: 8
Thanked 1,032 Times in 1,023 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
There you go ... good job!
Thanks for sharing the solution ... others might find this thread useful.
mlseim is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:05 PM.


Advertisement
Log in to turn off these ads.