diff --git a/Db.php b/MySQL/Db.php similarity index 100% rename from Db.php rename to MySQL/Db.php diff --git a/config.ini b/MySQL/config.ini similarity index 100% rename from config.ini rename to MySQL/config.ini diff --git a/db_functions.php b/MySQL/db_functions.php similarity index 100% rename from db_functions.php rename to MySQL/db_functions.php diff --git a/SQLite3/Db.php b/SQLite3/Db.php new file mode 100644 index 0000000..0f53b15 --- /dev/null +++ b/SQLite3/Db.php @@ -0,0 +1,101 @@ + 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 . "'"; + } +} diff --git a/SQLite3/README.md b/SQLite3/README.md new file mode 100644 index 0000000..5aae7de --- /dev/null +++ b/SQLite3/README.md @@ -0,0 +1,3 @@ +This is a SQLite3 version of the original MySQL database tutorial. + +The usage should be mostly the same diff --git a/SQLite3/config.ini b/SQLite3/config.ini new file mode 100644 index 0000000..acc5154 --- /dev/null +++ b/SQLite3/config.ini @@ -0,0 +1,3 @@ +[database] +database = test +encryption = thisisapassword diff --git a/SQLite3/db_functions.php b/SQLite3/db_functions.php new file mode 100644 index 0000000..b8f3dd7 --- /dev/null +++ b/SQLite3/db_functions.php @@ -0,0 +1,95 @@ + 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 . "'"; +}