Registers a "regular" User Defined Function for use in SQL statements (original) (raw)
sqlite_create_function
SQLiteDatabase::createFunction
(PHP 5 < 5.4.0, sqlite >= 1.0.0)
sqlite_create_function -- SQLiteDatabase::createFunction — Registers a "regular" User Defined Function for use in SQL statements
Description
sqlite_create_function ( resource $dbhandle
, string $function_name
, callable $callback
[, int $num_args
= -1 ] ) : void
public SQLiteDatabase::createFunction ( string $function_name
, callable $callback
[, int $num_args
= -1 ] ) : void
The UDF can be used in any SQL statement that can call functions, such as SELECT and UPDATE statements and also in triggers.
Parameters
dbhandle
The SQLite Database resource; returned from sqlite_open() when used procedurally. This parameter is not required when using the object-oriented method.
function_name
The name of the function used in SQL statements.
callback
Callback function to handle the defined SQL function.
Note: Callback functions should return a type understood by SQLite (i.e.scalar type).
num_args
Hint to the SQLite parser if the callback function accepts a predetermined number of arguments.
Note: Two alternative syntaxes are supported for compatibility with other database extensions (such as MySQL). The preferred form is the first, where the
dbhandle
parameter is the first parameter to the function.
Return Values
No value is returned.
Examples
Example #1 sqlite_create_function() example
`<?php
function md5_and_reverse($string)
{
return strrev(md5($string));
}
if (
dbhandle=sqliteopen(′mysqlitedb′,0666,dbhandle = sqlite_open('mysqlitedb', 0666, dbhandle=sqliteopen(′mysqlitedb′,0666,sqliteerror)) {sqlite_create_function($dbhandle, 'md5rev', 'md5_and_reverse', 1);$sql = 'SELECT md5rev(filename) FROM files'; rows=sqlitearrayquery(rows = sqlite_array_query(rows=sqlitearrayquery(dbhandle, $sql);
} else {
echo 'Error opening sqlite db: ' . $sqliteerror;
exit;
}
?> `
In this example, we have a function that calculates the md5 sum of a string, and then reverses it. When the SQL statement executes, it returns the value of the filename transformed by our function. The data returned in $rows
contains the processed result.
The beauty of this technique is that you do not need to process the result using a foreach loop after you have queried for the data.
PHP registers a special function named php
when the database is first opened. The php function can be used to call any PHP function without having to register it first.
Example #2 Example of using the PHP function
<?php <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>r</mi><mi>o</mi><mi>w</mi><mi>s</mi><mo>=</mo><mi>s</mi><mi>q</mi><mi>l</mi><mi>i</mi><mi>t</mi><msub><mi>e</mi><mi>a</mi></msub><mi>r</mi><mi>r</mi><mi>a</mi><msub><mi>y</mi><mi>q</mi></msub><mi>u</mi><mi>e</mi><mi>r</mi><mi>y</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">rows = sqlite_array_query(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.4306em;"></span><span class="mord mathnormal">ro</span><span class="mord mathnormal" style="margin-right:0.02691em;">w</span><span class="mord mathnormal">s</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1.0361em;vertical-align:-0.2861em;"></span><span class="mord mathnormal">s</span><span class="mord mathnormal" style="margin-right:0.01968em;">ql</span><span class="mord mathnormal">i</span><span class="mord mathnormal">t</span><span class="mord"><span class="mord mathnormal">e</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.1514em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">a</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mord mathnormal" style="margin-right:0.02778em;">rr</span><span class="mord mathnormal">a</span><span class="mord"><span class="mord mathnormal" style="margin-right:0.03588em;">y</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.1514em;"><span style="top:-2.55em;margin-left:-0.0359em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight" style="margin-right:0.03588em;">q</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.2861em;"><span></span></span></span></span></span></span><span class="mord mathnormal">u</span><span class="mord mathnormal" style="margin-right:0.03588em;">ery</span><span class="mopen">(</span></span></span></span>dbhandle, "SELECT php('md5', filename) from files"); ?>
This example will call the md5() on eachfilename
column in the database and return the result into $rows
Note:
For performance reasons, PHP will not automatically encode/decode binary data passed to and from your UDF's. You need to manually encode/decode the parameters and return values if you need to process binary data in this way. Take a look at sqlite_udf_encode_binary() and sqlite_udf_decode_binary() for more details.
Tip
It is not recommended to use UDF's to handle processing of binary data, unless high performance is not a key requirement of your application.