Jump to content

Problem with simultaneous connection


Perfidus

Recommended Posts

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??

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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');

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.