📄 function.pdo-sqlitecreatefunction.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Registers a User Defined Function for use in SQL statements</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="ref.pdo-sqlite.html">SQLite (PDO)</a></div> <div class="next" style="text-align: right; float: right;"><a href="refs.database.vendors.html">Vendor Specific Database Extensions</a></div> <div class="up"><a href="ref.pdo-sqlite.html">SQLite (PDO)</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div id="function.pdo-sqlitecreatefunction" class="refentry"> <div class="refnamediv"> <h1 class="refname">PDO->sqliteCreateFunction</h1> <p class="verinfo">(No version information available, might be only in CVS)</p><p class="refpurpose"><span class="refname">PDO->sqliteCreateFunction</span> — <span class="dc-title"> Registers a User Defined Function for use in SQL statements </span></p> </div> <div class="refsect1 description"> <h3 class="title">Description</h3> <div class="classsynopsis"> <div class="ooclass"><a href="class.pdo.html" class="classname">PDO</a></div> <div class="methodsynopsis dc-description"> <span class="type">bool</span> <span class="methodname"><b><b>sqliteCreateFunction</b></b></span> ( <span class="methodparam"><span class="type">string</span> <tt class="parameter">$function_name</tt></span> , <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.callback" class="type callback">callback</a></span> <tt class="parameter">$callback</tt></span> [, <span class="methodparam"><span class="type">int</span> <tt class="parameter">$num_args</tt></span> ] )</div> </div> <div class="warning"><b class="warning">Warning</b><p class="simpara">This function is<em class="emphasis">EXPERIMENTAL</em>. The behaviour of this function, its name, andsurrounding documentation may change without notice in a future release of PHP.This function should be used at your own risk.</p></div> <p class="para"> This method allows you to register a PHP function with SQLite as an <acronym title="User Defined Functions">UDF</acronym> (User Defined Function), so that it can be called from within your SQL statements. </p> <p class="para"> The UDF can be used in any SQL statement that can call functions, such as SELECT and UPDATE statements and also in triggers. </p> </div> <div class="refsect1 parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><i><tt class="parameter">function_name</tt></i></span> <dd> <p class="para"> The name of the function used in SQL statements. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">callback</tt></i></span> <dd> <p class="para"> Callback function to handle the defined SQL function. </p> <blockquote><p><b class="note">Note</b>: <span class="simpara"> Callback functions should return a type understood by SQLite (i.e. <a href="language.types.html#language.types.intro" class="link">scalar type</a>). </span> </p></blockquote> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">num_args</tt></i></span> <dd> <p class="para"> Hint to the SQLite parser if the callback function accepts a predetermined number of arguments. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns <b><tt>TRUE</tt></b> on success or <b><tt>FALSE</tt></b> on failure. </p> </div> <div class="refsect1 examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example"> <p><b>Example #1 <b>PDO::sqliteCreateFunction()</b> example</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">md5_and_reverse</span><span style="color: #007700">(</span><span style="color: #0000BB">$string</span><span style="color: #007700">) <br />{<br /> return </span><span style="color: #0000BB">strrev</span><span style="color: #007700">(</span><span style="color: #0000BB">md5</span><span style="color: #007700">(</span><span style="color: #0000BB">$string</span><span style="color: #007700">));<br />}<br /><br /></span><span style="color: #0000BB">$db </span><span style="color: #007700">= new </span><span style="color: #0000BB">PDO</span><span style="color: #007700">(</span><span style="color: #DD0000">'sqlite:sqlitedb'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$db</span><span style="color: #007700">-></span><span style="color: #0000BB">sqliteCreateFunction</span><span style="color: #007700">(</span><span style="color: #DD0000">'md5rev'</span><span style="color: #007700">, </span><span style="color: #DD0000">'md5_and_reverse'</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$rows </span><span style="color: #007700">= </span><span style="color: #0000BB">$db</span><span style="color: #007700">-></span><span style="color: #0000BB">query</span><span style="color: #007700">(</span><span style="color: #DD0000">'SELECT md5rev(filename) FROM files'</span><span style="color: #007700">)-></span><span style="color: #0000BB">fetchAll</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> <p class="para"> 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 <i><tt class="parameter">$rows</tt></i> contains the processed result. </p> <p class="para"> 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. </p> <div class="tip"><b class="tip">Tip</b> <p class="para"> You can use <a href="function.pdo-sqlitecreatefunction.html" class="xref">PDO->sqliteCreateFunction</a> and <a href="function.pdo-sqlitecreateaggregate.html" class="xref">PDO->sqliteCreateAggregate</a> to override SQLite native SQL functions. </p> </div> <blockquote><p><b class="note">Note</b>: This method is not available with the SQLite2 driver. Use the old style sqlite API for that instead. <br /> </p></blockquote> </div> <div class="refsect1 seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"><a href="function.pdo-sqlitecreateaggregate.html" class="xref">PDO->sqliteCreateAggregate</a></li> <li class="member"><a href="function.sqlite-create-function.html" class="function" rel="rdfs-seeAlso">sqlite_create_function()</a></li> <li class="member"><a href="function.sqlite-create-aggregate.html" class="function" rel="rdfs-seeAlso">sqlite_create_aggregate()</a></li> </ul> </p> </div></div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="ref.pdo-sqlite.html">SQLite (PDO)</a></div> <div class="next" style="text-align: right; float: right;"><a href="refs.database.vendors.html">Vendor Specific Database Extensions</a></div> <div class="up"><a href="ref.pdo-sqlite.html">SQLite (PDO)</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -