# a simple question about PHP extension: using user function in my own extension

- Thread: https://externals.io/message/64161
- Original archive: https://news-web.php.net/php.internals/64161
- Started by Amir on 2012-12-05T11:18:58+00:00
- Messages: 8
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #64161 — Amir — 2012-12-05T11:18:58+00:00

URL: https://externals.io/message/64161#64161

Hi
I am trying to build an PHP extension for my personal project.
For some reason. I want to build some part of my project with extension.
So, I can make a connection via my extension as you can see below my c++
source code.

Cause of poor document, I confused with zend engine source code and c++ and
relations with php.
I just have a simple question, how can I use mysql funtions work with query
and process it like:
mysql_query
mysql_fetch_assoc
and ...


here is my code:

PHP_FUNCTION(ig_connect_to_
database)
{
    zval fname, *args[3], dbLink;
    zval selectDB, *args_selectDB[1], retval;

    /*if(zend_call_method(NULL, NULL,
                                                    NULL,
                                                    "mysql_connect",

strlen("mysql_connect"),
                                                    &retval,
                                                    1,
                                                    args[1],
                                                    args[2] TSRMLS_CC
                                                                        )
== FAILURE) {
    php_printf("gosh!");
}
else {
    php_printf("yep!");
}*/


    ZVAL_STRING(&fname, "mysql_connect", 0);

    MAKE_STD_ZVAL(args[0]);
    ZVAL_STRING(args[0], "localhost", 1);

    MAKE_STD_ZVAL(args[1]);
    ZVAL_STRING(args[1], "root", 1);

    MAKE_STD_ZVAL(args[2]);
    ZVAL_STRING(args[2], "", 1);


    if (call_user_function(EG(function_table), NULL, &fname, &dbLink, 3,
args TSRMLS_CC) == FAILURE) {

        RETURN_STRING("fail to connect to database", 1);
    }
    else
    {
        zval_ptr_dtor(&args[2]);
        zval_ptr_dtor(&args[1]);
        zval_ptr_dtor(&args[0]);

        /*
        *return_value = dbLink;
        zval_copy_ctor(return_value);
        */

        /****now! I want to select a database on success****/

        ZVAL_STRING(&selectDB, "mysql_select_db", 0);

        MAKE_STD_ZVAL(args_selectDB[0]);
        ZVAL_STRING(args_selectDB[0], "clickbartarirg", 1);

        if (call_user_function(CG(function_table), NULL, &selectDB,
&retval, 1, args_selectDB TSRMLS_CC) == SUCCESS)
        {
            /* I successfully selected database */
            ZEND_FETCH_RESOURCE_NO_RETURN(retval);
            if(&retval)
            {
                php_printf("selected");
            }
            else
            {
                zend_error(E_ERROR, "failed to select database!");
            }

            zval_ptr_dtor(&args_selectDB[0]);

        }
        else
        {
            zend_error(E_ERROR, "database problem");
            zval_ptr_dtor(&args_selectDB[0]);
        }
    }

}

Thanks.

Regards
*Amir Ghazavi*
*Web Developer
IT Manager of ECDC2013*
*
*
*www.ecdcconference.org*

---

## #64162 — Pierre Joye — 2012-12-05T21:45:18+00:00

URL: https://externals.io/message/64161#64162 · In reply to: #64161

hi,


> Hi
> I am trying to build an PHP extension for my personal project.
> For some reason. I want to build some part of my project with extension.
> So, I can make a connection via my extension as you can see below my c++
> source code.
>
> Cause of poor document, I confused with zend engine source code and c++ and
> relations with php.
> I just have a simple question, how can I use mysql funtions work with query
> and process it like:
> mysql_query
> mysql_fetch_assoc
> and ...

I would suggest to look at the mysqli driver, using mysqlnd as backend
instead of calling user land functions. Mysqlnd is the common
underlying layer for all mysql extensions.


Cheers,
--
Pierre

@pierrejoye

---

## #64163 — Will Fitch — 2012-12-05T21:47:45+00:00

URL: https://externals.io/message/64161#64163 · In reply to: #64161

> Hi
> I am trying to build an PHP extension for my personal project.
> For some reason. I want to build some part of my project with extension.
> So, I can make a connection via my extension as you can see below my c++
> source code.
>
> Cause of poor document, I confused with zend engine source code and c++ and
> relations with php.
> I just have a simple question, how can I use mysql funtions work with query
> and process it like:
> mysql_query
> mysql_fetch_assoc
> and ...
>
>
Are you specifically referring to using mysql functions, or are you
referring to executing user space functions?  It looks like you're
specifically referring to MySQL functions, and in that case, you should
look at ext/mysqlnd or libmysql.  Executing user space functions in this
manner is not a good route to take.

In the PHP source directory, look at ext/mysqlnd/mysqlnd.h.  This will
provide you with a C-level API.


>
> here is my code:
>
> PHP_FUNCTION(ig_connect_to_
> database)
> {
>     zval fname, *args[3], dbLink;
>     zval selectDB, *args_selectDB[1], retval;
>
>     /*if(zend_call_method(NULL, NULL,
>                                                     NULL,
>                                                     "mysql_connect",
>
> strlen("mysql_connect"),
>                                                     &retval,
>                                                     1,
>                                                     args[1],
>                                                     args[2] TSRMLS_CC
>                                                                         )
> == FAILURE) {
>     php_printf("gosh!");
> }
> else {
>     php_printf("yep!");
> }*/
>
>
>     ZVAL_STRING(&fname, "mysql_connect", 0);
>
>     MAKE_STD_ZVAL(args[0]);
>     ZVAL_STRING(args[0], "localhost", 1);
>
>     MAKE_STD_ZVAL(args[1]);
>     ZVAL_STRING(args[1], "root", 1);
>
>     MAKE_STD_ZVAL(args[2]);
>     ZVAL_STRING(args[2], "", 1);
>
>
>     if (call_user_function(EG(function_table), NULL, &fname, &dbLink, 3,
> args TSRMLS_CC) == FAILURE) {
>
>         RETURN_STRING("fail to connect to database", 1);
>     }
>     else
>     {
>         zval_ptr_dtor(&args[2]);
>         zval_ptr_dtor(&args[1]);
>         zval_ptr_dtor(&args[0]);
>
>         /*
>         *return_value = dbLink;
>         zval_copy_ctor(return_value);
>         */
>
>         /****now! I want to select a database on success****/
>
>         ZVAL_STRING(&selectDB, "mysql_select_db", 0);
>
>         MAKE_STD_ZVAL(args_selectDB[0]);
>         ZVAL_STRING(args_selectDB[0], "clickbartarirg", 1);
>
>         if (call_user_function(CG(function_table), NULL, &selectDB,
> &retval, 1, args_selectDB TSRMLS_CC) == SUCCESS)
>         {
>             /* I successfully selected database */
>             ZEND_FETCH_RESOURCE_NO_RETURN(retval);
>             if(&retval)
>             {
>                 php_printf("selected");
>             }
>             else
>             {
>                 zend_error(E_ERROR, "failed to select database!");
>             }
>
>             zval_ptr_dtor(&args_selectDB[0]);
>
>         }
>         else
>         {
>             zend_error(E_ERROR, "database problem");
>             zval_ptr_dtor(&args_selectDB[0]);
>         }
>     }
>
> }
>
> Thanks.
>
> Regards
> *Amir Ghazavi*
> *Web Developer
> IT Manager of ECDC2013*
> *
> *
> *www.ecdcconference.org*

---

## #64197 — Amir — 2012-12-08T06:22:29+00:00

URL: https://externals.io/message/64161#64197 · In reply to: #64163

it is no mater using mysql function or other function. I want to use both
as function in my extension.

I used PHP 5.2.9 so i have lib mysql instead of mysqlnd.

Is there any way to use lib mysql in c-Level API?



Regards
*Amir Ghazavi*
*Web Developer
IT Manager of ECDC2013*
*
*
*www.ecdcconference.org*





> 
>
> > […]
> Are you specifically referring to using mysql functions, or are you
> referring to executing user space functions?  It looks like you're
> specifically referring to MySQL functions, and in that case, you should
> look at ext/mysqlnd or libmysql.  Executing user space functions in this
> manner is not a good route to take.
>
> In the PHP source directory, look at ext/mysqlnd/mysqlnd.h.  This will
> provide you with a C-level API.
>
>
> > […]

---

## #64213 — johannes@schlueters.de — 2012-12-10T00:25:08+00:00

URL: https://externals.io/message/64161#64213 · In reply to: #64197

Amir <admin@ecdcconference.org> wrote:

>it is no mater using mysql function or other function. I want to use
>both
>as function in my extension.

It matters as calling userspace functions is in at least 90% of the cases the wrong approach and there are better ones. The better approach depends on the goal.
(The only times where it is a good thing to do is when calling a user provided callback as in `usort()` or a method from an object extending an internal class s in some iterators etc.)

>I used PHP 5.2.9 so i have lib mysql instead of mysqlnd.

This is very old. You should update.

>Is there any way to use lib mysql in c-Level API?

libmysql is the MySQL Client Library. Refer to http://dev.mysql.com/doc/refman/5.5/en/c.html

johannes

---

## #64255 — Amir — 2012-12-11T14:25:43+00:00

URL: https://externals.io/message/64161#64255 · In reply to: #64213

>It matters as calling userspace functions is in at least 90% of the cases
the wrong approach and there are better ones. The better approach depends
on the goal.
(The only times where it is a good thing to do is when calling a user
provided callback as in `usort()` or a method from an object extending an
internal class s in some iterators etc.)

*please explain me more about using userspace function problem of if other
one, or example how can I use string API from zend engine, not in php.*

>This is very old. You should update.

*for some reson, I have to use PHP 5.2.9*

*everything is OK in PHP 5.3 but  when I compile my source code from PHP
5.2.9 source code I got error:*
microsoft visual studio 9.0\vc\include\sys\stat.inl(44) : error C2466:
cannot allocate an array of constant size 0

I did not find any appropriate answer for this error!



On Mon, Dec 10, 2012 at 3:55 AM, Johannes Schlüter
<johannes@schlueters.de>wrote:

>
>
> Amir <admin@ecdcconference.org> wrote:
>
> > […]
>
> It matters as calling userspace functions is in at least 90% of the cases
> the wrong approach and there are better ones. The better approach depends
> on the goal.
> (The only times where it is a good thing to do is when calling a user
> provided callback as in `usort()` or a method from an object extending an
> internal class s in some iterators etc.)
>
> > […]
>
> This is very old. You should update.
>
> > […]
>
> libmysql is the MySQL Client Library. Refer to
> http://dev.mysql.com/doc/refman/5.5/en/c.html
>
> johannes

---

## #64256 — Pierre Joye — 2012-12-11T14:29:11+00:00

URL: https://externals.io/message/64161#64256 · In reply to: #64255

hi,


> > […]
> the wrong approach and there are better ones. The better approach depends
> on the goal.
> (The only times where it is a good thing to do is when calling a user
> provided callback as in `usort()` or a method from an object extending an
> internal class s in some iterators etc.)
>
> *please explain me more about using userspace function problem of if other
> one, or example how can I use string API from zend engine, not in php.*
>
> > […]
>
> *for some reson, I have to use PHP 5.2.9*
>
> *everything is OK in PHP 5.3 but  when I compile my source code from PHP
> 5.2.9 source code I got error:*
> microsoft visual studio 9.0\vc\include\sys\stat.inl(44) : error C2466:
> cannot allocate an array of constant size 0
>
> I did not find any appropriate answer for this error!

Well, 5.2 is dead and never well supported VC9. The msdn documentation
will tell you how to fix this kind of error.

---

## #64257 — johannes@schlueters.de — 2012-12-11T15:15:26+00:00

URL: https://externals.io/message/64161#64257 · In reply to: #64255

> > […]
> 
> *please explain me more about using userspace function problem of if other
> one, or example how can I use string API from zend engine, not in php.*

There is no string API in Zend. Depending on what you want using C
standard library string functions is way more efficient and simpler
doable than calling through all the layers of PHP ...


johannes
