sklar.com

...composed of an indefinite, perhaps infinite number of hexagonal galleries...

© 1994-2017. David Sklar. All rights reserved.

myphp

myphp.so implements a MySQL UDF that interprets PHP code. Download myphp 0.1 here.

To build myphp.so, you need the libphp4.so library that is produced by building the PHP "embed" SAPI target. Add --enable-embed=shared to your PHP ./configure argument list. Run make and make install to build and install PHP.

Then, copy the php_embed.h header file (in the PHP distribution at sapi/embed/php_embed.h) to the myphp.cc source directory and run make to build the myphp.so library.

For MySQL to find the myphp.so library, both myphp.so and libphp4.so must be in a directory that your system's dynamic loader will look in. /usr/lib is a good choice. If you want to put them in a different directory, take a look at your system's ld.so(8) or ldconfig(8) man pages to see what your options are.

The MySQL Linux binary distribution provides a statically linked mysqld that can't dynamically load libraries, so it is not compatible with myphp.so. To use myphp.so on Linux, compile your own mysqld from the MySQL source distribution.

Once the two shared libraries are in an appropriate directory, tell MySQL about the function:

CREATE FUNCTION php RETURNS STRING SONAME 'myphp.so';

Now you can use the php() function just like any other MySQL built-in function. The first argument to php() is interpreted as PHP code. All of the arguments to PHP are stored in $argv.

PHP code that's just a value is returned:

mysql> SELECT php('$argv[1]','hello, world!');
+---------------------------------+
| php('$argv[1]','hello, world!') |
+---------------------------------+
| hello, world!                   |
+---------------------------------+
1 row in set (0.36 sec)

mysql> SELECT php('$argv[2]','ice cream','donuts');
+--------------------------------------+
| php('$argv[2]','ice cream','donuts') |
+--------------------------------------+
| donuts                               |
+--------------------------------------+
1 row in set (0.35 sec)

The same thing happens when you call a function:

mysql> SELECT php('strftime("%c")');
+--------------------------+
| php('strftime("%c")')    |
+--------------------------+
| Thu Oct 24 18:12:04 2002 |
+--------------------------+
1 row in set (0.36 sec)
		  

You can pass arguments to php() that are stored in a table:

mysql> CREATE TABLE test (s varchar(255));
mysql> insert into test values ('iguana'),('turtle'),('aardvark');
mysql> select s,php('strlen($argv[1])',s) FROM test;
+----------+---------------------------+
| s        | php('strlen($argv[1])',s) |
+----------+---------------------------+
| iguana   | 6                         |
| turtle   | 6                         |
| aardvark | 8                         |
+----------+---------------------------+
3 rows in set (1.07 sec)
		  

You can pass code to PHP() that is stored in a table:

mysql> create table code (name varchar(255), code mediumblob);
mysql> insert into code values ('capitalize','strtoupper($argv[1])');
mysql> select php(code,s) FROM test,code WHERE code.name = 'capitalize';
+-------------+
| php(code,s) |
+-------------+
| IGUANA      |
| TURTLE      |
| AARDVARK    |
+-------------+