?– Note The NULL character typically signals the end of a binary string, while 0x01 is the escape character
used within binary data. Therefore, to ensure that the escape character is properly interpreted by
the binary data parser, it needs to be decoded.
CHAPTER 22 ?– SQLITE 587
When you??™re using user-defined functions, a topic discussed in the next section,
you should never use this function. Instead, use the sqlite_udf_encode_binary() and
sqlite_udf_decode_binary() functions. Both are introduced in the next section.
Creating and Overriding SQLite Functions
An intelligent programmer will take every opportunity to reuse code. Because many
database-driven applications often require the use of a core task set, there are ample
opportunities to reuse code. Such tasks often seek to manipulate database data,
producing some sort of outcome based on the retrieved data. As a result, it would be
quite convenient if the task results could be directly returned via the SQL query, like so:
sqlite>SELECT convert_salary_to_gold(salary)
...> FROM employee WHERE empID=1";
PHP??™s SQLite library offers a means for registering and maintaining customized
functions such as this. This section shows you how it??™s accomplished.
Creating an SQLite Function
The sqlite_create_function() function enables you to register custom PHP functions
as SQLite user-defined functions (UDFs). Its prototype follows:
boolean sqlite_create_function(resource dbh, string func, mixed callback
[, int num_args])
For example, this function would be used to register the convert_salary_to_
gold() function discussed in the opening paragraphs of this section, like so:
// Define gold's current price-per-ounce
define("PPO",400);
// Calculate how much gold an employee can purchase with salary
function convert_salary_to_gold($salary)
{
return $salary / PPO;
}
588 CHAPTER 22 ?– SQLITE
// Connect to the SQLite database
$sqldb = sqlite_open("corporate.
Pages:
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672