[clug] PHP gurus? How can I find an "anonymous" mysql resource?

David Tulloh david at tulloh.id.au
Sat Jun 17 15:49:02 GMT 2006

I don't think that you can do much directly.  I had a quick scan through
the mysql extension source.  There is an internal variable that stores
the default link, upon making a DB connection this variable is set or
overwritten.  I couldn't see any way to retrieve this, nor could I see
any way to stop it being set.

I think that there are a few options to work around your problem:
    Create your DB connection first, the default will then be
overwritten but you can still access yours via a variable.
    Recreate the other connection once you have finished doing your stuff
    Use another DB library, there is MySQLi but I'm not sure if it can
coexist with MySQL.  There is also PDO.
    Write a small extension which can access the internal default
connection, allowing you to get/set it.


Tony and Robyn Lewis wrote:
> Pardon being OT, but I'm hoping someone on list can help with a PHP
> problem.
> We've got lots of hand-crafted PHP pages, and there is typically one DB
> connection per page.  Now we want to insert ("include") a common set of
> pages that, amongst other things, makes a different DB connection.
> Unfortunately, once our included code finishes, the existing MySQL
> connection is a goner, because it was never saved (as in, at
> mysql_connect() time), and up to now it worked, because mysql_query()
> will use the resource of the last mysql_connect().
> Is there a way I can essentially put the current MySQL resource (the
> variable associated with the connection) onto a stack (or just save it
> in a var), let the inserted code do its bit, and then restore / pop the
> old resource to be the current resource?
> Or is there a better way, other than rewriting the existing code to
> properly save and use the resource?
> Please, no sermons - this is inherited code, and I dislike PHP enough
> already.  I especially disrelish the idea of hand-editing scores of PHP
> pages looking for mysql calls.
> TIA,
> Tony

More information about the linux mailing list