Speed up development with full-stack environments for every branch.

Learn More

Register functions to use as sql aggregate or scalar [sqlite3 and PHP]

8 Runs 56 Views 14 Copies
Saved

Saved

gtsolutions 373

gtsolutions
published 4 years ago

<pre>
<?php
/*
 * PHP SQLite3 - Register functions to use as sql aggregate or scalar.
 */
//a file that creates and populates the table used in this example
require_once('createmyusers.php');

//Open the database mydb
$db = new SQLite3('db/mydb');

//Register the strtoupper() PHP function to use as an SQL scalar function
echo "List users with the username uppercased <br />";
$db->createFunction('strtoupper', 'strtoupper');
$results = $db->query('SELECT strtoupper(username),password FROM myusers');
while ($row = $results->fetchArray()) {
    var_dump($row);
}

//Register the strlen() PHP function to use as an SQL aggregate function
echo "List only users that have the username with at least 5 characters <br />";
$db->createFunction('strlen', 'strlen');
$results = $db->query('SELECT username,password FROM myusers WHERE strlen(username)>=5');
while ($row = $results->fetchArray()) {
    var_dump($row);
}

?>
Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).