Perfidus Posted March 27, 2009 Share Posted March 27, 2009 Hi there, I have a big function list that operates a DDBB to feed a website. At the top of this functions is an include which opens a connection to a DDBB and keeps it open. Only one function in that list needs to check another database so when needed I open a new connection, I get the data and close it. This connection has a different name than the first open connection which should be open all the time. (Or maybe shouldn't) function getData() { $link2 = mysql_connect('****', '***', '***'); if (!$link2) { die('Problem accesing BBDD: ' . mysql_error()); } ///I CHECK THE DATA } mysql_close($link2);/// I CLOSE THE CONNECTION AFTERWORDS } The problem comes when I close this connection because the other seems to be closed as well. Why? How can I handle this? Should I open and close a new connection everytime I check the DDBB?? Quote Link to comment Share on other sites More sharing options...
KPH71 Posted March 28, 2009 Share Posted March 28, 2009 I can't help much with just that - can you send the rest of the code e.g. the other connection function. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted March 28, 2009 Share Posted March 28, 2009 From the mysql_connect section of the php manual - If a second call is made to mysql_connect() with the same arguments, no new link will be established, but instead, the link identifier of the already opened link will be returned. You are probably reusing the existing link and mysql_close() is closing the single link. If the second database is on the same database server as the first and it has the same database username and password, there is no need to create a new connection. Just query the second database. Quote Link to comment Share on other sites More sharing options...
corbin Posted March 28, 2009 Share Posted March 28, 2009 PFMaBiSmAd's post hinted at this, but there is a known 'bug' with using different connections with the same credentials. I guess it's in the PHP core to protect idiots, or maybe it was an accident. What do I mean? Look: C:\Users\Corbin>php -r "$conn1 = mysql_connect('localhost', 'root', 'root'); $conn2 = mysql_connect('localhost', 'root', 'root'); var_dump($conn1); var_dump($conn2);" resource(4) of type (mysql link) resource(4) of type (mysql link) As you can see, the second call returns the same resource as the first call. If you ever do want two connections to the same host, an option is always to use a different host name: C:\Users\Corbin>php -r "$conn1 = mysql_connect('localhost', 'root', 'root'); $conn2 = mysql_connect('127.0.0.1', 'root', 'root'); var_dump($conn1); var_dump($conn2);" resource(4) of type (mysql link) resource(5) of type (mysql link) But as said in the post previous this one, you can query a different DB on the same conn. You don't even have to call mysql_select_db again really.... mysql_connect(); mysql_select_db('somedb'); mysql_query('SELECT * FROM someotherdb.sometable'); Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted March 28, 2009 Share Posted March 28, 2009 Should anyone need to consume available database connections, the 4th mysql_connect() parameter causes a new connection when using the same host/user/password. Quote Link to comment Share on other sites More sharing options...
corbin Posted March 28, 2009 Share Posted March 28, 2009 Whoa.... I never knew there was a 4th (and even a 5th! lol) param.... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.