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 2 of 2
  1. #1
    Regular Coder
    Join Date
    Jun 2007
    Posts
    310
    Thanks
    86
    Thanked 3 Times in 3 Posts

    Max connections help please

    Hello All,

    I'm looking for some advice with my connection script to my database.

    In my content management system I have a page (initial_connect.php) that is included on all my pages in my content management system.

    It contains two connections to different mysql databases via functions (but those databases sit on the same server but I can't have them together in one DB). One to get the initial data from a function called initial_connect and one to get all my news items called news_connect

    PHP Code:
    <?php

    /*****************************************************************
     - INITIAL CONNECTION
    ******************************************************************/

    function initial_connect() {

    $result mysql_connect("localhost""usernamehere""passwordhere");
        
        if (!
    $result) {    return FALSE; }
        
        if (!
    mysql_select_db("1STdatabasename")) { return FALSE; }
        
        return 
    $result;
    }


    /*****************************************************************
     - GET INDIVIDUAL SITE DETAILS FROM connections TABLE
    ******************************************************************/
    initial_connect();

    $SQL "SELECT * FROM connection_details'";
    $result mysql_query($SQL) or die (mysql_error());

    $siteDetails mysql_fetch_array($result);
     
    $sitename $siteDetails['name'];
    $googleCode $siteDetails['google'];
    //etc


    /*****************************************************************
     - CONNECT TO THE NEWS DATABASE
    ******************************************************************/

    function news_connect() {

    $result mysql_connect("localhost""usernamehere""passwordhere");
        
        if (!
    $result) {    return FALSE; }
        
        if (!
    mysql_select_db("2NDdatabasename")) { return FALSE; }
        
        return 
    $result;
    }
    ?>

    Now, thoughout all my pages in my content management system I include initial_connect.php like so, and also open the connection to the news_connect() function and have about 25 pages in my CMS.

    PHP Code:
    <?php

    include ('initial_connect.php'); 

    // Connect to news database
    news_connect();

    //Rest of code
    But i'm getting several errors on my site saying I have Too many connections

    Is there anything I can do, or change in my code to fix this, or make it work more efficiently

    Thanks

    Chris
    Last edited by stfc_boy; 05-07-2009 at 06:07 PM.

  • #2
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,519
    Thanks
    114
    Thanked 110 Times in 109 Posts
    Hi. If both databases on the same server then you don't need to connect twice. If the two databases are on different servers then you need to use the forth argument in mysql_connect to true. As it says here link.

    e.g.

    PHP Code:
    mysql_connect("localhost""usernamehere""passwordhere"true); 
    Thats if they are on different servers. If they are not then you just connect once and just use

    PHP Code:
    mysql_select_db("2NDdatabasename"); 
    To change database. The mysql connect is for connecitons to the server the mysql_select_db is to select the different databases.

    Just out of curiosity why can't you use the same database?
    You can not say you know how to do something, until you can teach it to someone else.


  •  

    Posting Permissions

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