FAQ
Hi all,

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?

Btw, thank you all for developing what is, by far, my preferred language.

Thanks in advance.
Mauro Nicolás Infantino.

Search Discussions

  • Sara Golemon at Nov 30, 2006 at 6:43 am

    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
  • Mauro N. Infantino at Nov 30, 2006 at 11:19 pm
    Sara,

    Sorry if the question was answered before, I will try to look harder next
    time.

    Just for the record:

    - I ended up doing what you suggested but in MINIT. Given the facts you
    provided, I'll change it to RINIT.

    - The php_mysql.h file doesn't include the constants for the resource name,
    so I defined a constant in my own code.

    - The php_mysql_conn type is not defined in the .h file, so it was also
    replicated.

    You're again right about the inclusion of the function in Appendix A, I was
    worried if this usage was "correct" enough.
    Again, thank you all very much for your work and specially to you Sara for
    sharing your knowledge.

    Regards,
    Mauro.
    -----Original Message-----
    From: Sara Golemon
    Sent: Thursday, November 30, 2006 3:42 AM
    To: Mauro N. Infantino
    Cc: internals@lists.php.net
    Subject: [PHP-DEV] Re: Cross-extension resource

    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

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupphp-internals @
categoriesphp
postedNov 27, '06 at 5:01p
activeNov 30, '06 at 11:19p
posts3
users2
websitephp.net

People

Translate

site design / logo © 2022 Grokbase