I'm writing an extension in order to achieve better performance in a
specific module from our application. Right now, I'm trying to use an
already established mysql connection (with mysql_connect) in our extension,
so we don't have to connect to the database twice, since that connection is
used in the PHP code. I don't need PHP's mysql API, since I can do
everything in C, I just want the connection ID. I've read Sara's (great...
GREAT) book, but I couldn't find any specific solution to this situation. Is
this possible? Is there a "best" way?
Surprising how often this question comes up....
/* True global to store local copy of mysql's le_link */
static int local_mysql_le = -1;
PHP_RINIT_FUNCTION(myext)
{
if (local_mysql_le == -1) {
local_mysql_le = zend_fetch_list_dtor_id("mysql link");
}
return SUCCESS;
}
A few notes about this solution:
(1) I'm doing this in RINIT because, prior to PHP 5.1, there's no way to
enforce load order for shared extensions, so it's possible that your ext
will load prior to mysql and therefore the list ID won't have been
registered during the time of MINIT. If you know that MySQL will always
be loaded prior to your extension (either because of local policy or
because you're only targeting 5.1 or later -- which has module
dependencies), then you can do it once in MINIT and be done with it.
(2) The name "mysql link" is case sensitive and must match the resource
type name you're looking for precisely. There's also no guard against
the (unlikely) possibility that some other extension registers a
resource named "mysql link" which isn't actually a MySQL link.
(3) You should guard against the possibility that no matching list id
will be found for that name (perhaps MySQL isn't loaded), so be sure to
put some error checking in there... Hint: This function returns 0 on
failure.
(4) You'll probably need to fetch "mysql link persistent" as well...
I think this function is covered in Appendix A, but the cat is on my lap
and I can't reach the bookshelf from here....
-Sara
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php