function find_my_page() { // get the name of the 'missing' file $page = basename($_SERVER['REDIRECT_URL']); // used to pick the correct dictionary $dir = dirname($_SERVER['REDIRECT_URL']); $key = md5($dir); // load spelling dictionaries $ps = pspell_config_create("en"); pspell_config_personal($ps, "./{$key}.pws"); $pl = pspell_new_config($ps); // find alternatives $alt = pspell_suggest($pl, $page); if (!$alt) { // no matches, no choice but to show site map display_site_map(); return; } // escape data for sqlite foreach ($alt as $key => $file) { $alt[$key] = sqlite_escape_string($file); } // fetch all matching pages; $db = new sqlite_db("./typos.sqlite"); $alt = $db->single_query("SELECT url FROM typo WHERE key IN('" . implode("','", $alt) . "')"); switch (@count($alt)) { case 1: // if only one suggestion is avaliable redirect the user to that page header("Location: {$alt[0]}"); return; break; case 0: // no matches, no choice but to show site map display_site_map(); break; default: // show the user possible alternatives if >1 is found echo "The page you requested, '{$_SERVER['REDIRECT_URL']}' cannot be found.<br />\nDid you mean:\n"; foreach ($alt as $url) { echo " <a href='{$url}'>{$url}</a><br />\n"; } } }
<?php // open database $db = new sqlite_db("./ip.db"); // begin transaction $db->query("BEGIN"); $fp = fopen("./ip-to-country.csv", "r"); $query_str = ''; while ($row = fgetcsv($fp, 4096)) { foreach ($row as $key => $val) { // secure data $row[$key] = sqlite_escape_string($val); } // check for existance of a country in db if (!($country_id = $db->single_query("SELECT id FROM country_data WHERE cc_code_2='{$cc}'"))) { // add new country if (!$db->query("INSERT INTO country_data \n\t\t\t(cc_code_2, cc_code_3, country_name) \n\t\t\tVALUES('{$row[2]}', '{$row[3]}', '{$row[4]}')")) { // fetch error $err_code = $db->last_error(); printf("Query Failed %d:%s\n", $err_code, sqlite_error_string($err_code)); exit; } // get ID for the last inserted row $country_id = $db->last_insert_rowid(); } $query_str .= "INSERT INTO ip_ranges \n\t\t\t(ip_start, ip_end, country_code)\n\t\t\tVALUES({$row[0]}, {$row[1]}, {$country_id});"; } // insert IP data via a chained query $db->query($query_str); // finalize transaction $db->query("COMMIT");
<?php function most_similar(&$context, $string, $source_str) { /* Calculate the similarity between two strings */ $sim = similar_text($string, $source_str); if (empty($context) || $sim > $context['sim']) { $context = array('sim' => $sim, 'title' => $string); } } function most_similar_finalize(&$context) { return $context['title']; } chdir(dirname(__FILE__)); $db = new sqlite_db("./db2.sqlite"); /* * sqlite function name * PHP logic function * PHP finalize function * number of arguments not counting the context (optional) */ $db->create_aggregate('similar_text', 'most_similar', 'most_similar_finalize', 2); $title = 'mesg #2'; echo "Most Similar title to '{$title}' according to similar_text() is: "; echo $db->single_query("SELECT similar_text(title, '{$title}') FROM messages"); echo "<br />\n";
<pre> <?php $db = new sqlite_db(dirname(__FILE__) . "/db.sqlite"); $res = $db->query("SELECT login FROM auth_tbl"); while ($login = $res->fetch_single()) { echo $login . "\n"; } print_r($db->single_query("SELECT login FROM auth_tbl")); ?> </pre>