function sqlite_error_hadler($errno, $errstr, $errfile, $errline, $args) { // Open bugs database $err_db = new sqlite_db("php_errors"); $error_hash = md5($errstr, $errfile, $errline, $errno); // check if previous errors of the same nature, had already occured, // if they did update the error counter. $res = $err_db->query("UPDATE bugs_db SET \n\t\t\terror_counter=error_counter+1 \n\t\t\tWHERE b_hash='{error_hash}'"); // we got a hit, nothing more to do if ($res->changes()) { // close bug db unset($err_db); return; } // prepare data for sql insertion $errstr = sqlite_escape_string($errstr); $errfile = sqlite_escape_string($errfile); $errline = (int) $errline; $errno = (int) $errno; $args = sqlite_escape_string(implode(', ', $args)); // Uh Oh, new error, let's log it. $err_db->query("INSERT INTO bugs_db\n\t\t(b_hash, error_counter, b_errstr, \n\t\tb_errfile, b_errline, b_errno, b_args)\n\t\tVALUES(\n\t\t\t'{$error_hash}',\n\t\t\t1,\n\t\t\t'{$errstr}',\n\t\t\t'{$errfile}',\n\t\t\t{$errline},\n\t\t\t{$errno},\n\t\t\t'{$args}'\n\t\t)"); // close bug db unset($err_db); }
function four_oh_four_handler($type, $severity) { // determine if the link leading to the not found page is remote or local if (!empty($_SERVER['HTTP_REFERER'])) { $remote_or_local = (int) preg_match("!http://[^/]*{$_SERVER['HTTP_HOST']}!", $_SERVER['HTTP_REFERER']); } else { $remote_or_local = 2; // direct request } $db = new sqlite_db("error_log"); $db->query("INSERT INTO web_errors\n\t\t(host, request_url, referrer_url, user_ip, \n\t\t user_browser, notes, request_method,\n\t\t severity, source, time, type)\n\t\tVALUES(\n\t\t '" . sqlite_escape_string($_SERVER['HTTP_HOST']) . "',\n\t\t '" . sqlite_escape_string($_SERVER['REDIRECT_URL']) . "',\n\t\t '" . sqlite_escape_string($_SERVER['HTTP_REFERER']) . "',\n\t\t '" . sqlite_escape_string($_SERVER['REMOTE_ADDR']) . "',\n\t\t '" . sqlite_escape_string($_SERVER['HTTP_USER_AGENT']) . "',\n\t\t '" . sqlite_escape_string($_SERVER['REDIRECT_ERROR_NOTES']) . "',\n\t\t '" . sqlite_escape_string($_SERVER['REDIRECT_REQUEST_METHOD']) . "',\n\t\t '{$severity}',\n\t\t {$remote_or_local},\n\t\t " . time() . ",\n\t\t '{$type}'\n\t\t)\n\t"); }
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"; } } }
<pre> <?php $db = new sqlite_db(dirname(__FILE__) . "/db.sqlite"); foreach ($db->query("SELECT * FROM auth_tbl", SQLITE_NUM) as $row) { print_r($row); } ?> </pre>
<pre> <?php $db = new sqlite_db(dirname(__FILE__) . "/ip.db"); $res = $db->unbuffered_query("SELECT * FROM country_data WHERE cc_code_2='CA'", SQLITE_ASSOC); foreach ($res as $row) { print_r($row); } ?> </pre>
<?php $db = new sqlite_db(dirname(__FILE__) . "/ip.db"); $r = $db->array_query("SELECT * FROM sqlite_master LIMIT 1", SQLITE_ASSOC); echo '<pre>' . print_r($r, true) . '</pre>';
<?php $db = new sqlite_db(dirname(__FILE__) . "/ip.db"); $r = $db->fetch_column_types("ip_ranges"); echo '<pre>' . print_r($r, true) . '</pre>';
<?php $db = new sqlite_db(":memory:"); $db->query("CREATE TABLE foobar (misc CHAR(10))"); $db->query("INSERT INTO foobar (misc) VALUES('Tall')"); $db->query("\n\tINSERT INTO foobar (misc) VALUES('Wez');\n\tINSERT INTO foobar (misc) VALUES('Marcus');\n\tINSERT INTO foobar (misc) VALUES('Ilia');\n\tUPDATE foobar SET misc='Tal' WHERE misc='Tall';\n"); /* When chained queries are used rows changed returns the * cumulative of all rows affected by executed queries. */ echo $db->changes() . "rows affected by chained query.";
<?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 $db = new sqlite_db(":memory:"); $db->query("CREATE TABLE foobar (misc CHAR(10))"); $db->query("INSERT INTO foobar (misc) VALUES('Tall')"); $db->query("\n\t\tINSERT INTO foobar (misc) VALUES('Wez');\n\t\tINSERT INTO foobar (misc) VALUES('Marcus');\n\t\tINSERT INTO foobar (misc) VALUES('Ilia');\n\t\tUPDATE foobar SET misc='Tal' WHERE misc='Tall';\n\t"); /* When chained queries are used rows changed returns the cumulative * of all rows affected by executed queries. */ echo "Number of affected rows: " . $db->changes() . " by chained query.<br />\n";
<?php try { /* code to monitor for errors */ $db = new sqlite_db("new_db"); $result = $db->query("SELECT ...", SQLITE_ASSOC); while ($row = $result->fetch_object()) { /* output code */ } } catch (sqlite_exception $err) { /* error handling */ echo "Message: " . $err->getMessage() . "\n"; echo "File: " . $err->getFile() . "\n"; echo "File: " . $err->getCode() . "\n"; echo "Line: " . $err->getLine() . "\n"; print_r($err->getTrace()); /* backtrace array */ echo "BackTrace: " . $err->getTraceAsString() . "\n"; }
<?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";
<?php $db = new sqlite_db(":memory:"); $db->query("CREATE TABLE foobar (id INTEGER PRIMARY KEY, misc CHAR(10))"); $db->query("INSERT INTO foobar (misc) VALUES('Marcus');\n\t\t\tINSERT INTO foobar (misc) VALUES('Ilia')"); /* When performing multiple inserts within a single query * only the id of very last insert is returned */ echo "Last id: " . $db->last_insert_rowid() . "<br />\n"; echo '<pre>'; print_r($db->array_query("SELECT * FROM foobar", SQLITE_ASSOC)); echo '</pre>';
<?php function encode($str) { return str_pad(strlen($str), 10, "0", STR_PAD_LEFT) . $str; } if (!empty($_GET['q'])) { $query = sqlite_escape_string($_GET['q']); /* Query SQLite database */ $db = new sqlite_db("my_db.sqlite"); $result = $db->array_query("SELECT id, descr FROM bug_db WHERE dev='{$query}'"); /* If there are results, encode them using IPIP and send to client */ if ($rows = count($result)) { echo str_pad($rows, 10, "0", STR_PAD_LEFT); foreach ($result as $ent) { echo encode($ent['id']) . encode($ent['descr']); } exit; } } echo str_repeat("0", 10);
<?php chdir(dirname(__FILE__)); // pres2 hack // sample IP $_SERVER['REMOTE_ADDR'] = "24.100.195.79"; $ip_int = sprintf("%u", ip2long($_SERVER['REMOTE_ADDR'])); try { $db = new sqlite_db("./ip.db"); } catch (sqlite_exception $err) { die($err->getMessage() . " in " . $err->getFile() . ":" . $err->getLine()); } $res = $db->unbuffered_query("\nSELECT country_name \nFROM ip_ranges ir \nINNER JOIN country_data cd ON ir.country_code=cd.id\nWHERE {$ip_int} BETWEEN ip_start AND ip_end"); echo "User is located in " . $res->fetch_single();
<?php require "./cache.php"; // our cache code // Simple guestbook script. $db = new sqlite_db("gb.sqlite"); $r = $db->array_query("SELECT * FROM guestbook", SQLITE_ASSOC); foreach ($r as $row) { echo $r->user . ' wrote on ' . date("Ymd", $r->date) . ":<br />\n"; echo $r->message . "<hr /><hr />"; }
<?php $db = new sqlite_db(dirname(__FILE__) . "/db.sqlite"); $res = $db->query("SELECT * FROM auth_tbl"); /* return number of rows selected */ echo "Rows Fetched: " . $res->num_rows() . "<br />\n"; /* determine the number of columns in the result set */ echo "Columns per row " . ($n_cols = $res->num_fields()) . "<br />\n"; for ($i = 0; $i < $n_cols; $i++) { /* fetch column names */ echo "Column Names: " . $res->field_name($i) . "<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>