PhpRiot
News Archive
PhpRiot Newsletter
Your Email Address:

More information

Direct MySQL Stream Access - Revised

Note: This article was originally published at Planet PHP on 26 September 2011.
Planet PHP

Roughly three years ago I was writing about Direct MySQL Stream Access - a way to access the low-level stream PHP's mysqlnd library is using. Back then this had been a patch against PHP's mysqli extension. As such a feature is quite dangerous (you can easily mess with the connection state which confuses mysqlnd and/or the MySQL server) we didn't push it into the main PHP tree. Now three years later it's time to look at this again as we don't need to patch PHP anymore.

Since the mentioned patch was written mysqlnd got a plugin interface about which I was talking before. This plugin-interface, especially in the version of PHP 5.4, makes it trivial to implement this feature.

PHP_FUNCTION(mysqlnd_to_stream) { AAA zval *conn_zv; AAA MYSQLND *conn; AAA if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &conn_zv) == FAILURE) { AAAAAAA return; AAA } AAA if (!(conn = zval_to_mysqlnd(conn_zv))) { AAAAAAA php_error_docref(NULL TSRMLS_CC, E_WARNING, AAAAAAAAAAAAAAAAAAA "Passed variable is no mysqlnd based connection"); AAAAAAA RETURN_FALSE; AAA } AAA php_stream_to_zval(conn-net-stream, return_value); } A

If you take a function like the one shown above and add some general PHP infrastructure you are done. The key function here is the function MYSQLND* zval_to_mysqlnd(zval *connection) which takes a PHP variable as parameter and in case it is a MySQL connection (ext/mysql, mysqli or pdo_mysql) will return the corresponding MYSQLND pointer which gives access to the stream which then has to be packed into a PHP variable, again. The nice thing, compared to the old version is not only that it is a plugin which can be loaded into PHP as shared extension via php.ini but also that it works with all MySQL extensions, not only mysqli as the one before.

You can download the complete source, but be warned: This is experimental stuff and not supported in any way, but I hope good enough to get a feeling what's possible with mysqlnd.