Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
File renamed without changes.
File renamed without changes.
101 changes: 101 additions & 0 deletions SQLite3/Db.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

/**
* Database wrapper for SQLite3
*/
class Db {
// Database connection
protected static $connection;

/**
* Connect to the database
*
* @return bool False on failure / SQLite3 object instance on success
*/
public function connect() {
// Check if connection is already set
if( isset( self::$connection ) ) {
return self::$connection;
}

// Check if last connect failed
if( self::$connection === false ) {
return false;
}

// Start connecting
try {
$config = parse_ini_file( './config.ini' );
$perm = SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE;
self::$connection = new SQLite3( $config['database'] , $perm , $config['encryption'] );
} catch ( Exception $e ) {
var_dump( $e );
return false;
}
return self::$connection;
}

/**
* Query the database
*
* @param $query The query string
* @result bool False on failure / mixed The result of the SQLite3::query() function
*/
public function query( $query ) {
// Get connection
$connection = $this -> connect();

// Check if valid connection
if( $connection === false ) {
return false;
}

// Query the database
$result = $connection -> query( $query );

return $result;
}

/**
* Fetch rows from the database (SELECT query)
*
* @param $query The query string
* @return bool False on failure / array Database rows on success
*/
public function select( $query ) {
$rows = array();
$result = $this -> query( $query );

if( $result === false ) {
return false;
}

while( $row = $result -> fetchArray() ) {
$rows[] = $row;
}

return $rows;
}

/**
* Fetch the last error from the database
*
* @return string Database error message
*/
public function error() {
$connection = $this -> connect();
return $connection -> lastErrorMsg();
}

/**
* Quote and escape value for use in a database query
*
* @param string $value The value to be quoted and escaped
* @return string The quoted and escaped string
*/
public function quote( $value ) {
$connection = $this -> connect();
$escaped = $connection -> escapeString( $value );
return "'" . $escaped . "'";
}
}
3 changes: 3 additions & 0 deletions SQLite3/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
This is a SQLite3 version of the original MySQL database tutorial.

The usage should be mostly the same
3 changes: 3 additions & 0 deletions SQLite3/config.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[database]
database = test
encryption = thisisapassword
95 changes: 95 additions & 0 deletions SQLite3/db_functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php
/**
* Connect to the database
*
* @return bool false on failure / mysqli MySQLi object instance on success
*/
function db_connect() {
// Define the connection as static variable
static $connection;

// Check if connection is already set
if( isset( $connection ) ) {
return $connection;
}

// Check if last connect failed
if( $connection === false ) {
return false;
}

// Start connecting
try {
$config = parse_ini_file( './config.ini' );
$perm = SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE;
$connection = new SQLite3( $config['database'] , $perm , $config['encryption'] );
} catch ( Exception $e ) {
var_dump( $e );
return false;
}
return $connection;
}

/**
* Query the database
*
* @param $query The query string
* @return mixed The result of the mysqli::query() function
*/
function db_query( $query ) {
// Get connection
$connection = db_connect();

// Check if valid connection
if( $connection === false ) {
return false;
}

// Query the database
$result = $connection -> query( $query );

return $result;
}

/**
* Fetch rows from the database (SELECT query)
*
* @param $query The query string
* @return bool False on failure / array Database rows on success
*/
function db_select($query) {
$rows = array();
$result = db_query( $query );

if( $result === false ) {
return false;
}

while( $row = $result -> fetchArray() ) {
$rows[] = $row;
}

return $rows;
}

/**
* Fetch the last error from the database
*
* @return string Database error message
*/
function db_error() {
$connection = db_connect();
return $connection -> lastErrorMsg();
}

/**
* Quote and escape value for use in a database query
*
* @param string $value The value to be quoted and escaped
* @return string The quoted and escaped string
*/
function db_quote( $value ) {
$connection = db_connect();
$escaped = $connection -> escapeString( $value );
return "'" . $escaped . "'";
}