...

View Full Version : Max connections help please



stfc_boy
05-07-2009, 05:26 PM
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

/*****************************************************************
- 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

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

timgolding
05-08-2009, 09:41 AM
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 (http://uk.php.net/manual/en/function.mysql-connect.php).

e.g.



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



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?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum