ฟังก์ชัน sqlite_create_function()
ความหมายของฟังก์ชันและการใช้งาน
การสร้างชุดฟังก์ชัน PHP กับ SQLite เป็น UDF ด้วยฟังก์ชัน sqlite_create_function()
รูปแบบการเขียน (Syntax)
- การสร้างชุดฟังก์ชัน PHP กับ SQLite เป็น UDF เพื่อที่จะสามารถเรียกใช้ฟังก์ชั่นต่าง ๆ ได้ภายในคำสั่ง SQL
void sqlite_create_function ( resource $dbhandle , string $function_name , callable $callback [, int $num_args = -1 ] )
Object oriented style (method):
public void SQLiteDatabase::createFunction ( string $function_name , callable $callback [, int $num_args = -1 ] )
ตัวอย่าง
<?php
function md5_and_reverse($string)
{
return strrev(md5($string));
}
if ($dbhandle = sqlite_open('mysqlitedb', 0666, $sqliteerror)) {
sqlite_create_function($dbhandle, 'md5rev', 'md5_and_reverse', 1);
$sql = 'SELECT md5rev(filename) FROM files';
$rows = sqlite_array_query($dbhandle, $sql);
} else {
echo 'Error opening sqlite db: ' . $sqliteerror;
exit;
}
?>
ตัวอย่าง การเรียกใช้ฟังก์ชั่น
<?php
$rows = sqlite_array_query($dbhandle, "SELECT php('md5', filename) from files");
?>