Skip to content

Instantly share code, notes, and snippets.

@bgulla
Created January 15, 2014 20:35
Show Gist options
  • Save bgulla/8444041 to your computer and use it in GitHub Desktop.
Save bgulla/8444041 to your computer and use it in GitHub Desktop.
<?php
/**
Creates the sqlite database file
*/
function createDB(){
try{
//$db = getDBObject();
$db = new SQLite3('db/prax0rs.db') or die('Unable to open database');
$sql = "
CREATE TABLE IF NOT EXISTS users(
id INTEGER PRIMARY KEY AUTOINCREMENT,
username STRING,
password STRING,
record_date DATETIME DEFAULT CURRENT_TIMESTAMP);";
$db->exec($sql) or die('Unable to create Table users.');
}catch (Exception $e){
die($e->getMessage());
}
}
/**
Prevents mysql-injection.
*/
function sanitize($var){
return $var;
}
/**
Inserts a record into the sqlite db.
*/
function insertRecord( $username, $pw){
$db = new SQLite3('db/prax0rs.db') or die('Unable to open database');
$username = sanitize($username);
$pw = sanitize($pw);
$sql = "INSERT INTO users(username,password) VALUES ('$username','$pw');";
try{
$db->exec($sql);
}catch (Exception $e){
die($e->getMessage());
}
}
/**
*/
function getAllRecords(){
try{
$db = new SQLite3('db/prax0rs.db') or die('Unable to open database');
$sql = "SELECT * FROM users;";
$ret = $db->query($sql) or die('Unable to query DB');
// or die("Unable to query db");
while($row = $ret->fetchArray()){
echo $row['id'] . ",";
echo $row['username'] . ",";
echo $row['password'] . ",";
echo $row['record_date'] . ",";
echo "\n";
}
}catch (Exception $e){
die($e->getMessage());
}
}
//createDB();
echo "Inserting Records... \n";
insertRecord("bieber@aol.com","iLikeTurtles");
echo "\n\nPrinting Current Records...\n";
getAllRecords();
echo "\n\nend.";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment