Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    Sep 2007
    Posts
    809
    Thanks
    5
    Thanked 2 Times in 2 Posts

    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

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,122
    Thanks
    2
    Thanked 327 Times in 319 Posts
    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.

  • #3
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,526
    Thanks
    8
    Thanked 1,091 Times in 1,082 Posts
    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.

  • #4
    Regular Coder
    Join Date
    Sep 2007
    Posts
    809
    Thanks
    5
    Thanked 2 Times in 2 Posts
    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!

  • #5
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,526
    Thanks
    8
    Thanked 1,091 Times in 1,082 Posts
    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.

  • #6
    Regular Coder
    Join Date
    Sep 2007
    Posts
    809
    Thanks
    5
    Thanked 2 Times in 2 Posts
    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 12:24 PM.

  • #7
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,526
    Thanks
    8
    Thanked 1,091 Times in 1,082 Posts
    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.

  • #8
    Regular Coder
    Join Date
    Sep 2007
    Posts
    809
    Thanks
    5
    Thanked 2 Times in 2 Posts
    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'];
    ?>

  • #9
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,526
    Thanks
    8
    Thanked 1,091 Times in 1,082 Posts
    There you go ... good job!
    Thanks for sharing the solution ... others might find this thread useful.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •