PHP: How Do I Get a MySQL Link Identifier For an Already Open Connection?

10,088

Solution 1

Thanks to everyone for the replies.

I ended up opening my own connection, and when my plug-in was done, the last line reopens the initial connection so the rest of the main script can use the database it expects. It's sloppy, I know, but it doesn't seem like there's a better option in this case. :/

Update:: I've submitted this as a feature request on php.net. The link is: http://bugs.php.net/bug.php?id=49400

Solution 2

Even though the explanation of Anti Veeranna is correct, there is a very straight-forward way to do what you want. If you were to read the documentation for mysql_connect(), you would find out that this function has a 4th parameter, "new_link" , which is false by default. If you call mysql_connect() again with the same parameters, which is probably the case, it would not create a new connection, but instead would return the resource identifier of the old one.

Any time ;)

Solution 3

No, there isn't. You have to record link identifiers yourself. You could define link identifier as a class property, so that you can easily access it from your methods and don't have to worry about passing it as a variable over and over again.

Share:
10,088
Nick
Author by

Nick

Updated on June 13, 2022

Comments

  • Nick
    Nick almost 2 years

    When you open a MySQL connection with mysql_connect(), it returns a link identifier. But what if you want to get that link identifier again later in the script? (for example: A plug-in, that needs to open a new database connection, and still access the old one.)

    I'm looking for a way to return a link identifier to the last connection opened by mysql_connect(). Is there a function that does this?

  • Amber
    Amber over 14 years
    I don't think that's actually what they want - I think they're wanting to open a db connection to a different server, but not overwrite any existing "current last opened db resource" that a script which includes their plugin might have been relying on.
  • nikc.org
    nikc.org over 14 years
    As I understand it, the original question was "How can I reuse a previously opened DB connection, without explicitly recording the connection identifier". If I'm wrong, then I am, but that's how I see it.
  • nikc.org
    nikc.org over 14 years
    Ah, I see now that you're correct. However, mysql_pconnect will still do what is requested, since it identifies connections according to host, username and password. If you need to include the database into the "uniqueness", you're better off using a custom class for connection pooling, as I suggested. I can't really see why I got a downvote.
  • Amber
    Amber over 14 years
    It won't do what is requested - the scenario here is that someone else's script is doing mysql_connect (or pconnect) and not saving the resource link, but instead trusting that the 'most recently opened resource' is the one that they opened. If the OP's plugin uses pconnect for a different host, the 'most recently opened resource' will change to the new resource id, even though the old resource will still be around - but the original script that's relying on the 'most recent' resource being its own will be broken since that's no longer the case.
  • nikc.org
    nikc.org over 14 years
    In that case, it's simply a design flaw (IMHO). If you have an architecture that allows plugins but doesn't provide a mechanism for accessing the database, you should redesign. But I suppose you're right and my solution is not what was sought after.
  • mpen
    mpen almost 10 years
    Downvote for linking to LMGTFY. I got to this answer through Google. A non-broken hyperlink would haven't hurt.
  • XedinUnknown
    XedinUnknown almost 10 years
    @Mark, you found this answer through Google. Why is LMGTFY hurting your search? The hyperlink isn't broken.
  • mpen
    mpen almost 10 years
    @XedinUnknown For all intents and purposes, it is. I found this answer on Google when searching for a way to get the link identifier, and then foolishly clicked your link thinking it would take me to php.net. Instead, I got a smartass answer that makes me wait 30 seconds just so that you could taunt the OP.
  • XedinUnknown
    XedinUnknown almost 10 years
    On the other hand, it did take a few minutes of my life to check and document this method here. If you are so worried about your 30 seconds, why not do exactly what the LMGTFY link demonstrates? After all, that's the whole point of LMGTFY.