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 make_page_lookup_dict($dir) { $old_dir = chdir($dir); // generate dictionary key $key = md5($dir); // create dictionary where the spelling alternatives will be stored $ps = pspell_config_create("en"); pspell_config_personal($ps, "./{$key}.pws"); $pl = pspell_new_config($ps); // fetch all files inside the directory $files = glob("*"); // open db for URL storage $db = new sqlite_db("./typos.sqlite"); $db->query("CREATE TABLE typo (url, key)"); // append names to dictionary foreach ($files as $file) { if (preg_match('!^([A-Za-z]+)!', $file, $match)) { pspell_add_to_personal($pl, $match[1]); $db->query("INSERT INTO typo (url, key)\n\t\t\t\tVALUES(\n\t\t\t\t '" . sqlite_escape_string($dir . "/" . $file) . "',\n\t\t\t\t '" . sqlite_escape_string($match[0]) . "')\n\t\t\t\t"); } } // save dictionary pspell_save_wordlist($pl); }
<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>
<?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 $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"; }
<?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 $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 // 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");
<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>