Skip to content

Instantly share code, notes, and snippets.

@motyar
Created February 2, 2024 09:07
Show Gist options
  • Save motyar/09a38c8d2be9756b95aefb1463df6c88 to your computer and use it in GitHub Desktop.
Save motyar/09a38c8d2be9756b95aefb1463df6c88 to your computer and use it in GitHub Desktop.
SQLite class PHP
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
class db
{
public $file;
public $res;
public $query;
public $statement;
public function __construct($file) {
// exit('sss here');
$this->file = $file;
if (!file_exists($this->file)) {
$errorMessage = 'DB connect error. Unable to create database file. ';
$errorMessage .= 'Run the following commands to create the file and set permissions: ' . PHP_EOL;
$errorMessage .= '<pre>touch ' . $this->file.' && chmod 660 ' . $this->file.' && chown www-data:www-data '.$this->file.'</pre>';
$errorMessage.='<pre>chown www-data:www-data "folderin which db is"</pre>';
throw new Exception($errorMessage);
}
$this->connect();
}
public function connect() {
$this->res = new SQLite3($this->file);
if (! $this->res) {
throw new Exception('DB connect error, check file location and permissions');
}
}
root@vps:~/www/ropuz.com# cat includes/db.php
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
class db
{
public $file;
public $res;
public $query;
public $statement;
public function __construct($file) {
// exit('sss here');
$this->file = $file;
if (!file_exists($this->file)) {
$errorMessage = 'DB connect error. Unable to create database file. ';
$errorMessage .= 'Run the following commands to create the file and set permissions: ' . PHP_EOL;
$errorMessage .= '<pre>touch ' . $this->file.' && chmod 660 ' . $this->file.' && chown www-data:www-data '.$this->file.'</pre>';
$errorMessage.='<pre>chown www-data:www-data "folderin which db is"</pre>';
throw new Exception($errorMessage);
}
$this->connect();
}
public function connect() {
$this->res = new SQLite3($this->file);
if (! $this->res) {
throw new Exception('DB connect error, check file location and permissions');
}
}
/*
public function Q($query, $params = []) {
$this->query = $query;
$this->statement = $this->res->prepare($query) or die($this->res->lastErrorMsg());
if ($this->statement) {
if (!empty($params)) {
foreach ($params as $key => $value) {
$this->statement->bindValue($key + 1, $value);
}
}
$result = $this->statement->execute();
$type = strtolower(explode(' ', trim($query))[0]);
if ($type=='select') {
$result = $this->res->query($query) or die($this->res->lastErrorMsg());
if ($result->numColumns() > 0) {
$data = [];
while ($record = $result->fetchArray()) {
$data[] = $record;
}
return $data;
}
}
elseif ($type=='insert') {
$result = $this->res->exec($query) or die($this->res->lastErrorMsg());
return $this->res->lastInsertRowID();
} else {
return $result;
}
}
return false;
}
*/
public function Q($query, $params = []) {
$this->query = $query;
$this->statement = $this->res->prepare($query) or die($this->res->lastErrorMsg());
if ($this->statement) {
if (!empty($params)) {
foreach ($params as $key => $value) {
$this->statement->bindValue($key + 1, $value);
}
}
$result = $this->statement->execute();
$type = strtolower(explode(' ', trim($query))[0]);
if ($type=='select') {
$data = [];
while ($record = $result->fetchArray()) {
$data[] = $record;
}
return $data;
}
elseif ($type=='insert') {
return $this->res->lastInsertRowID();
} else {
return $result;
}
}
return false;
}
}
?>
<?php
try {
$db = new db('database/database.db');
print_r($db);
$db->Q('CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY, name TEXT)');
// All other queries
$db->Q('INSERT INTO test (name) VALUES ('John Doe')');
$data = $db->Q('SELECT * FROM test');
echo '<pre>'; print_r($data);
} catch (Exception $e) {
echo 'Error: ' . $e->getMessage();
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment