forked from Mowie/Database-Class
Initial Commit
This commit is contained in:
commit
7b742d0386
137
db-blank.php
Executable file
137
db-blank.php
Executable file
|
@ -0,0 +1,137 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Min PHP Version: 4
|
||||||
|
*/
|
||||||
|
|
||||||
|
class db
|
||||||
|
{
|
||||||
|
//Init
|
||||||
|
private $host;
|
||||||
|
private $dbname;
|
||||||
|
private $usr;
|
||||||
|
private $pass;
|
||||||
|
private $dbh;
|
||||||
|
private $prefix;
|
||||||
|
public $data;
|
||||||
|
|
||||||
|
//Datenbankverbindung aufbauen
|
||||||
|
function __construct($host, $dbname, $usr, $pass, $prefix = '')
|
||||||
|
{
|
||||||
|
$this->host = $host;
|
||||||
|
$this->dbname = $dbname;
|
||||||
|
$this->usr = $usr;
|
||||||
|
$this->pass = $pass;
|
||||||
|
$this->prefix = $prefix;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private $col = null;
|
||||||
|
|
||||||
|
public function setCol($col)
|
||||||
|
{
|
||||||
|
$this->col = $col;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Daten holen
|
||||||
|
public function get($where = [], $link = 'AND')
|
||||||
|
{
|
||||||
|
if (isset($this->col))
|
||||||
|
{
|
||||||
|
//Entweder übergebene Daten oder in $this->data vorhandene nutzen
|
||||||
|
if (empty($where))
|
||||||
|
{
|
||||||
|
if (empty($this->data))
|
||||||
|
{
|
||||||
|
$where = [];
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
$where = $this->data;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Daten einfügen
|
||||||
|
public function insert($args = [])
|
||||||
|
{
|
||||||
|
if (isset($this->col))
|
||||||
|
{
|
||||||
|
//Entweder übergebene Daten oder in $this->data vorhandene nutzen
|
||||||
|
if (empty($args))
|
||||||
|
{
|
||||||
|
if (empty($this->data))
|
||||||
|
{
|
||||||
|
$args = [];
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
$args = $this->data;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($args))
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function lastID()
|
||||||
|
{
|
||||||
|
return $this->dbh->lastInsertId();
|
||||||
|
}
|
||||||
|
|
||||||
|
//Daten Updaten
|
||||||
|
public function update($where = [], $dataToUpdate = [], $link = 'AND')
|
||||||
|
{
|
||||||
|
if (isset($this->col))
|
||||||
|
{
|
||||||
|
//Entweder übergebene Daten oder in $this->data vorhandene nutzen
|
||||||
|
if (empty($dataToUpdate))
|
||||||
|
{
|
||||||
|
if (empty($this->data))
|
||||||
|
{
|
||||||
|
$dataToUpdate = [];
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
$dataToUpdate = $this->data;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Daten Löschen
|
||||||
|
public function delete($where = [], $link = 'AND')
|
||||||
|
{
|
||||||
|
if (isset($this->col))
|
||||||
|
{
|
||||||
|
//Entweder übergebene Daten oder in $this->data vorhandene nutzen
|
||||||
|
if (empty($where))
|
||||||
|
{
|
||||||
|
if (empty($this->data))
|
||||||
|
{
|
||||||
|
$where = [];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
$where = $this->data;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Aufräumen
|
||||||
|
public function clear()
|
||||||
|
{
|
||||||
|
$this->col = null;
|
||||||
|
$this->data = '';
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
276
db-mysql.php
Executable file
276
db-mysql.php
Executable file
|
@ -0,0 +1,276 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Min PHP Version: 4
|
||||||
|
*/
|
||||||
|
|
||||||
|
class db
|
||||||
|
{
|
||||||
|
//Init
|
||||||
|
private $host;
|
||||||
|
private $dbname;
|
||||||
|
private $usr;
|
||||||
|
private $pass;
|
||||||
|
private $dbh;
|
||||||
|
private $prefix;
|
||||||
|
public $data;
|
||||||
|
|
||||||
|
//Datenbankverbindung aufbauen
|
||||||
|
function __construct($host, $dbname, $usr, $pass, $prefix = '')
|
||||||
|
{
|
||||||
|
$this->host = $host;
|
||||||
|
$this->dbname = $dbname;
|
||||||
|
$this->usr = $usr;
|
||||||
|
$this->pass = $pass;
|
||||||
|
$this->prefix = $prefix;
|
||||||
|
|
||||||
|
$this->dbh = new PDO('mysql:host=' . $host . ';dbname=' . $dbname, $usr, $pass);
|
||||||
|
|
||||||
|
//UTF-8
|
||||||
|
$this->dbh->exec("SET NAMES 'utf8'");
|
||||||
|
$this->dbh->exec("SET CHARACTER SET 'utf8'");
|
||||||
|
}
|
||||||
|
|
||||||
|
private $col = null;
|
||||||
|
|
||||||
|
public function setCol($col)
|
||||||
|
{
|
||||||
|
$this->clear();
|
||||||
|
$this->col = $col;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Daten holen
|
||||||
|
public function get($where = [], $link = 'AND')
|
||||||
|
{
|
||||||
|
if (isset($this->col))
|
||||||
|
{
|
||||||
|
//Entweder übergebene Daten oder in $this->data vorhandene nutzen
|
||||||
|
if (empty($where))
|
||||||
|
{
|
||||||
|
if (empty($this->data))
|
||||||
|
{
|
||||||
|
$where = [];
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
$where = $this->data;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Where zusamenbauen
|
||||||
|
$whereCl = '';
|
||||||
|
$whereAr = [];
|
||||||
|
if (!empty($where))
|
||||||
|
{
|
||||||
|
$i = 1;
|
||||||
|
$whereCount = count($where);
|
||||||
|
$whereCl = ' WHERE ';
|
||||||
|
foreach ($where as $col => $val)
|
||||||
|
{
|
||||||
|
$whereCl .= $col . ' = ?';
|
||||||
|
$whereAr[] = $val;
|
||||||
|
if ($i < $whereCount) $whereCl .= ' ' . $link . ' ';
|
||||||
|
$i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//print_r($whereAr);
|
||||||
|
|
||||||
|
$stmt = $this->dbh->prepare('SELECT * FROM ' . $this->prefix . $this->col . $whereCl);
|
||||||
|
$stmt->execute($whereAr);
|
||||||
|
|
||||||
|
$all = [];
|
||||||
|
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
|
||||||
|
{
|
||||||
|
$all[] = $row;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->data = '';
|
||||||
|
/*$cnt = count($all);
|
||||||
|
|
||||||
|
if ($cnt == 1)
|
||||||
|
{
|
||||||
|
$this->data = $all[0];
|
||||||
|
return $all[0];
|
||||||
|
} else
|
||||||
|
{*/
|
||||||
|
$this->data = $all;
|
||||||
|
return $all;
|
||||||
|
//}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Daten einfügen
|
||||||
|
public function insert($args = [])
|
||||||
|
{
|
||||||
|
if (isset($this->col))
|
||||||
|
{
|
||||||
|
//Entweder übergebene Daten oder in $this->data vorhandene nutzen
|
||||||
|
if (empty($args))
|
||||||
|
{
|
||||||
|
if (empty($this->data))
|
||||||
|
{
|
||||||
|
$args = [];
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
$args = $this->data;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($args))
|
||||||
|
{
|
||||||
|
$stmt = 'INSERT INTO ' . $this->prefix . $this->col . ' (`';
|
||||||
|
$i = 1;
|
||||||
|
$vals = [];
|
||||||
|
$valCnt = '';
|
||||||
|
foreach ($args as $key => $val)
|
||||||
|
{
|
||||||
|
$stmt .= $key.'`';
|
||||||
|
//$vals[] = utf8_encode($val);
|
||||||
|
$vals[] = $val;
|
||||||
|
$valCnt .= '?';
|
||||||
|
if ($i < count($args))
|
||||||
|
{
|
||||||
|
$stmt .= ', `';
|
||||||
|
$valCnt .= ', ';
|
||||||
|
}
|
||||||
|
$i++;
|
||||||
|
}
|
||||||
|
$stmt .= ') VALUES (' . $valCnt . ')';
|
||||||
|
//echo $stmt;
|
||||||
|
|
||||||
|
$insert = $this->dbh->prepare($stmt);
|
||||||
|
return $insert->execute($vals);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function lastID()
|
||||||
|
{
|
||||||
|
return $this->dbh->lastInsertId();
|
||||||
|
}
|
||||||
|
|
||||||
|
//Daten Updaten
|
||||||
|
public function update($where = [], $dataToUpdate = [], $link = 'AND')
|
||||||
|
{
|
||||||
|
if (isset($this->col))
|
||||||
|
{
|
||||||
|
//Entweder übergebene Daten oder in $this->data vorhandene nutzen
|
||||||
|
if (empty($dataToUpdate))
|
||||||
|
{
|
||||||
|
if (empty($this->data))
|
||||||
|
{
|
||||||
|
$dataToUpdate = [];
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
$dataToUpdate = $this->data;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//echo mb_detect_encoding($dataToUpdate['alias']);
|
||||||
|
//print_r($dataToUpdate);
|
||||||
|
|
||||||
|
$stmt = 'UPDATE ' . $this->prefix . $this->col . ' SET ';
|
||||||
|
$vals = [];
|
||||||
|
$i = 1;
|
||||||
|
foreach ($dataToUpdate as $key => $val)
|
||||||
|
{
|
||||||
|
$stmt .= $key . ' = ?';
|
||||||
|
//$val = utf8_encode($val);
|
||||||
|
//$vals[] = utf8_encode($val);
|
||||||
|
$vals[] = $val;
|
||||||
|
//echo mb_detect_encoding($val).' -> '.$val;
|
||||||
|
if ($i < count($dataToUpdate)) $stmt .= ', ';
|
||||||
|
$i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Where zusamenbauen
|
||||||
|
$whereCl = '';
|
||||||
|
$whereAr = [];
|
||||||
|
if (!empty($where))
|
||||||
|
{
|
||||||
|
$i = 1;
|
||||||
|
$whereCount = count($where);
|
||||||
|
$whereCl = ' WHERE ';
|
||||||
|
foreach ($where as $col => $val)
|
||||||
|
{
|
||||||
|
$whereCl .= $col . ' = ?';
|
||||||
|
$vals[] = $val;
|
||||||
|
if ($i < $whereCount) $whereCl .= ' ' . $link . ' ';
|
||||||
|
$i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$stmt .= $whereCl;
|
||||||
|
|
||||||
|
//secho $stmt;
|
||||||
|
$update = $this->dbh->prepare($stmt);
|
||||||
|
return $update->execute($vals);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Daten Löschen
|
||||||
|
public function delete($where = [], $link = 'AND')
|
||||||
|
{
|
||||||
|
if (isset($this->col))
|
||||||
|
{
|
||||||
|
//Entweder übergebene Daten oder in $this->data vorhandene nutzen
|
||||||
|
if (empty($where))
|
||||||
|
{
|
||||||
|
if (empty($this->data))
|
||||||
|
{
|
||||||
|
$where = [];
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
$where = $this->data;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$stmt = 'DELETE FROM ' . $this->prefix . $this->col;
|
||||||
|
//Where zusamenbauen
|
||||||
|
$whereCl = '';
|
||||||
|
$whereAr = [];
|
||||||
|
$vals = [];
|
||||||
|
if (!empty($where))
|
||||||
|
{
|
||||||
|
$i = 1;
|
||||||
|
$whereCount = count($where);
|
||||||
|
$whereCl = ' WHERE ';
|
||||||
|
foreach ($where as $col => $val)
|
||||||
|
{
|
||||||
|
$whereCl .= $col . ' = ?';
|
||||||
|
$vals[] = $val;
|
||||||
|
if ($i < $whereCount) $whereCl .= ' ' . $link . ' ';
|
||||||
|
$i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$stmt .= $whereCl;
|
||||||
|
|
||||||
|
//echo $stmt;
|
||||||
|
$delete = $this->dbh->prepare($stmt);
|
||||||
|
return $delete->execute($vals);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Version
|
||||||
|
public function version()
|
||||||
|
{
|
||||||
|
$STH = $this->dbh->query('SELECT VERSION( ) AS version');
|
||||||
|
$STH->setFetchMode(PDO::FETCH_OBJ);
|
||||||
|
if ($row = $STH->fetch())
|
||||||
|
{
|
||||||
|
return $row->version;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Aufräumen
|
||||||
|
public function clear()
|
||||||
|
{
|
||||||
|
$this->col = null;
|
||||||
|
$this->data = '';
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
26
example.php
Executable file
26
example.php
Executable file
|
@ -0,0 +1,26 @@
|
||||||
|
<?php
|
||||||
|
require_once 'db-mysql.php';
|
||||||
|
|
||||||
|
$db = new db('localhost', 'testdb', 'root', 'supersecretpassword');
|
||||||
|
$db->setCol('blog');
|
||||||
|
//$db->data['id'] = 13;
|
||||||
|
//$db->get();
|
||||||
|
//print_r($db->get());
|
||||||
|
//print_r($db->data);
|
||||||
|
|
||||||
|
|
||||||
|
/*$db->data['titel'] = 'testdbclas';
|
||||||
|
$db->data['alias'] = '---d-d-d-d-';
|
||||||
|
$db->insert();*/
|
||||||
|
//if($db->insert(['titel' => 'testclass', 'inhalt' => 'baum', 'alias' => '0000003030498-g-dfghd-f'])) echo 'yay';
|
||||||
|
|
||||||
|
/*$db->data['titel'] = 'GAadsfhganz Neu';
|
||||||
|
$db->data['inhalt'] = 'gabs noch net';*/
|
||||||
|
//$db->update(['id' => 12]);
|
||||||
|
//if($db->update(['titel' => 'wat anderes', 'inhalt' => 'meh'], ['id' => 7])) echo 'update!';
|
||||||
|
|
||||||
|
//$db->data['id'] = 9;
|
||||||
|
//$db->delete();
|
||||||
|
//if($db->delete(['id' => 6])) echo 'del';
|
||||||
|
|
||||||
|
$db->clear();
|
136
readme.de.md
Normal file
136
readme.de.md
Normal file
|
@ -0,0 +1,136 @@
|
||||||
|
#Datenbankklasse
|
||||||
|
|
||||||
|
Diese Datenbankklasse soll einen vereinfachten Umgang beim Arbeiten mit Datenbanken bieten. Dies wird dadurch erreicht, dass die oft umständlichen Funktionen in vier Hauptfunktionen gebündelt werden:
|
||||||
|
|
||||||
|
## Initialisieren
|
||||||
|
|
||||||
|
Zuerst muss eine Datenbankverbindung hergestellt werden:
|
||||||
|
`$db = new db('server', 'database', 'user', 'pass', 'prefix');`
|
||||||
|
Der letzte Pararmeter ist Optional. Wenn angegeben, wird er bei jedem Aufruf einer der Funktion vor den Tabellennamen gestellt.
|
||||||
|
|
||||||
|
Um anschließend die Methoden nutzen zu können, müssen wir noch sagen, um welche Tabelle es geht:
|
||||||
|
`$db->setCol('Blog');`
|
||||||
|
|
||||||
|
## Daten übergeben
|
||||||
|
|
||||||
|
Daten können bei allen Methoden entweder direkt als Pararmeter an die Funktion übergeben werden, oder aber vorher in `$db->data` mit den Selben Optionen deklariert werden. Die übergebenen Pararmeter haben höhere Priorität, d.h. wenn sowohl Parameter dirket an die Funktion übergeben werden als auch in `$db->data` Daten festgelegt werden, werden die Daten verwendet, die direkt übergeben wurden.
|
||||||
|
|
||||||
|
##get()
|
||||||
|
|
||||||
|
Diese Funktion wird dazu genutzt, Datensätze aus der Datenbank zu holen, sie gibt ein Array mit den Inhalten zurück.
|
||||||
|
|
||||||
|
Liefert ein Array mit den Daten zurück.
|
||||||
|
|
||||||
|
Wenn nur ein Datensatz gefunden wurde, wird dieser driekt zurückgegeben. Andernfalls werden die Datensätze als Einträge in einem Array zurückgegeben. Alle Daten sind immer in `$db->data` verfügbar.
|
||||||
|
|
||||||
|
### Syntax
|
||||||
|
|
||||||
|
`$db->get($where = [], $link = 'AND')`
|
||||||
|
|
||||||
|
`$where` Ein Array, mit welchem Schlüsselwörter bennant und mit Werten befüllt werden können. Wenn vorhanden, werden nur die Werte zurückgegeben, auf welche die angegebenen Werte zutreffen. Schema: `['Spalte' => 'Wert']`
|
||||||
|
`$link` Wenn über `$where` mehr als ein Schlüssel übergeben wird, lässt sich über diesen Pararmeter die Art der Verknüpfung definieren. Standard ist 'AND'.
|
||||||
|
|
||||||
|
## insert()
|
||||||
|
|
||||||
|
Mit dieser Funktion können Daten in die Datenbank eingefügt werden.
|
||||||
|
|
||||||
|
Gibt `true` zurück, wenn die Daten erfolgreich eingefügt wurden. Andernfalls `false`.
|
||||||
|
|
||||||
|
### lastID()
|
||||||
|
|
||||||
|
Nachdem diese Funktion ausgeführt wurde, gibt die Funktion `lastID()` die ID des zuletzt eingefügten Datensatzes zurück.
|
||||||
|
|
||||||
|
### Syntax
|
||||||
|
|
||||||
|
`insert($args = [])`
|
||||||
|
|
||||||
|
`$args` Ein Array, welches die Spalten mit dazugehörigen Daten enthält. Schema: `['Spalte' => 'Daten']`
|
||||||
|
|
||||||
|
## update()
|
||||||
|
|
||||||
|
Mit dieser Funktion werden bereits vorhandene Daten in der Datenbank geändert.
|
||||||
|
|
||||||
|
Gibt `true` zurück, wenn die Daten erfolgreich geändert wurden. Andernfalls `false`.
|
||||||
|
|
||||||
|
### Syntax
|
||||||
|
|
||||||
|
`update($where = [], $dataToUpdate = [], $link = 'AND')`
|
||||||
|
|
||||||
|
`$where` Ein Array, mit welchem Schlüsselwörter bennant und mit Werten befüllt werden können. Wenn vorhanden, werden nur die Datensätze geändert, auf welche die Schlüsselwörter und Werte zutreffen. **Ansonsten werden alle Datensätze in der Tabelle geändert!** Schema: `['Spalte' => 'Wert']`
|
||||||
|
`$dataToUpdate` Ein Array, der Daten, welche geändert werden sollen. Schema: `['Spalte' => 'Neuer Inhalt']`
|
||||||
|
`$link` Wenn über `$where` mehr als ein Schlüssel übergeben wird, lässt sich über diesen Pararmeter die Art der Verknüpfung definieren. Standard ist 'AND'.
|
||||||
|
|
||||||
|
## delete()
|
||||||
|
|
||||||
|
Mit dieser Funktion können Daten aus der Datenbank gelöscht werden.
|
||||||
|
|
||||||
|
Gibt `true` zurück, wenn die Daten erfolgreich geändert wurden. Andernfalls `false`.
|
||||||
|
|
||||||
|
### Syntax
|
||||||
|
|
||||||
|
`delete($where = [], $link = 'AND')`
|
||||||
|
|
||||||
|
`$where` Ein Array, mit welchem Schlüsselwörter bennant und mit Werten befüllt werden können. Wenn vorhanden, werden nur die Datensätze gelöscht, auf welche die Schlüsselwörter und Werte zutreffen. **Ansonsten werden alle Datensätze in der Tabelle gelöscht!** Schema: `['Spalte' => 'Wert']`
|
||||||
|
`$link` Wenn über `$where` mehr als ein Schlüssel übergeben wird, lässt sich über diesen Pararmeter die Art der Verknüpfung definieren. Standard ist 'AND'.
|
||||||
|
|
||||||
|
## clear()
|
||||||
|
|
||||||
|
Mit dieser Funktion wird die aktuelle Collection auf null gesetzt, damit kann man bequem auf eine andere Datenbank zugreifen.
|
||||||
|
|
||||||
|
# Beispiele
|
||||||
|
|
||||||
|
## Daten Holen
|
||||||
|
|
||||||
|
`$db = new db('localhost', 'testdeb', 'root', '123456789');`
|
||||||
|
`$db->setCol('blog');`
|
||||||
|
`$db->data['id'] = 3;`
|
||||||
|
`print_r($db->get());`
|
||||||
|
|
||||||
|
_Oder:_
|
||||||
|
|
||||||
|
`$db = new db('localhost', 'testdeb', 'root', '123456789');`
|
||||||
|
`$db->setCol('blog');`
|
||||||
|
`print_r($db->get(['id' => 3]));`
|
||||||
|
|
||||||
|
Dies wird dieselben Daten zurückliefern.
|
||||||
|
|
||||||
|
## Daten einfügen
|
||||||
|
|
||||||
|
`$db = new db('localhost', 'testdeb', 'root', '123456789');`
|
||||||
|
`$db->setCol('blog');`
|
||||||
|
`$db->data['title'] = 'test';`
|
||||||
|
`$db->data['content'] = 'Lorem Ipsum...';`
|
||||||
|
`if($db->insert()) echo 'Daten wurden eingefügt';`
|
||||||
|
|
||||||
|
_Oder:_
|
||||||
|
|
||||||
|
`$db = new db('localhost', 'testdeb', 'root', '123456789');`
|
||||||
|
`$db->setCol('blog');`
|
||||||
|
`if($db->insert(['title' => 'test', 'content' => 'Lorem Ipsum...'])) echo 'Daten wurden eingefügt';`
|
||||||
|
|
||||||
|
## Daten Ändern
|
||||||
|
|
||||||
|
`$db = new db('localhost', 'testdeb', 'root', '123456789');`
|
||||||
|
`$db->setCol('blog');`
|
||||||
|
`$db->data['title'] = 'GAaanz Neu';`
|
||||||
|
`$db->data['content'] = 'gabs noch net';`
|
||||||
|
`$db->update(['id' => 10]);`
|
||||||
|
|
||||||
|
_Oder:_
|
||||||
|
|
||||||
|
`$db = new db('localhost', 'testdeb', 'root', '123456789');`
|
||||||
|
`$db->setCol('blog');`
|
||||||
|
`if($db->update(['title' => 'GAaanz Neu', 'content' => 'gabs noch net'], ['id' => 10])) echo 'Daten wurden erfolgreich geändert.';`
|
||||||
|
|
||||||
|
## Daten löschen
|
||||||
|
|
||||||
|
`$db = new db('localhost', 'testdeb', 'root', '123456789');`
|
||||||
|
`$db->setCol('blog');`
|
||||||
|
`$db->data['id'] = 9;`
|
||||||
|
`if($db->delete()) echo 'Daten wurden erfolgreich gelöscht';`
|
||||||
|
|
||||||
|
_Oder:_
|
||||||
|
|
||||||
|
`$db = new db('localhost', 'testdeb', 'root', '123456789');`
|
||||||
|
`$db->setCol('blog');`
|
||||||
|
`if($db->delete(['id' => 9])) echo 'Daten wurden erfolgreich gelöscht';`
|
183
readme.md
Executable file
183
readme.md
Executable file
|
@ -0,0 +1,183 @@
|
||||||
|
# PHP Database-Class
|
||||||
|
|
||||||
|
This Database-Class is made to make handling databases (and database-operations) in PHP more easy.
|
||||||
|
|
||||||
|
##Other Databases
|
||||||
|
|
||||||
|
You can add support for any other database by using the file `db-blank.php`. I'm currently working to support mongodb.
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
Examples can be found in `examples.php` or in the "Examples"-Section of this document.
|
||||||
|
|
||||||
|
## Initialize
|
||||||
|
|
||||||
|
First, you need to create an connection to your database:
|
||||||
|
```php
|
||||||
|
$db = new db('server', 'database', 'user', 'password', 'prefix');
|
||||||
|
```
|
||||||
|
The last pararmeter is optional. If provided, it will be put in front of each table name. This is useful if you have multiple applications in one database.
|
||||||
|
|
||||||
|
To use the methods, you need to first specify the table:
|
||||||
|
|
||||||
|
```php
|
||||||
|
$db->setCol('Blog');
|
||||||
|
```
|
||||||
|
|
||||||
|
## Provide Data
|
||||||
|
|
||||||
|
You can provide data either directly in the function (via pararmeters) or before via `$db->data` with the same options.
|
||||||
|
Parameters you provide directly in the function, are of higher priority as data provided via `$db->data`.
|
||||||
|
|
||||||
|
##get()
|
||||||
|
|
||||||
|
Get data with this function, it returns an array with the data.
|
||||||
|
|
||||||
|
All data is available via `$db->data`.
|
||||||
|
|
||||||
|
### Syntax
|
||||||
|
|
||||||
|
`$db->get($where = [], $link = 'AND')`
|
||||||
|
|
||||||
|
`$where`
|
||||||
|
An array to return specific data. If provided, only data which matches is returnend. Scheme: `['key' => 'value']`.
|
||||||
|
|
||||||
|
You can also provde the data via `$db->data['key'] = 'value'`.
|
||||||
|
|
||||||
|
`$link`
|
||||||
|
If you provide more than one key, you can choose via this pararmeter how to connect them, currently via `AND` or `OR`. Standard is `AND`.
|
||||||
|
|
||||||
|
To see it in action, take a look at `example.php` or the "Examples"-Section at the bottom of this document.
|
||||||
|
|
||||||
|
## insert()
|
||||||
|
|
||||||
|
Inserts data provided via `$db->data` or directly via the function.
|
||||||
|
|
||||||
|
Returns `true` on success, otherwise `false`.
|
||||||
|
|
||||||
|
### lastID()
|
||||||
|
|
||||||
|
After running `$db->insert()`, this function returns the ID of the lastly inserted Data.
|
||||||
|
|
||||||
|
### Syntax
|
||||||
|
|
||||||
|
`insert($args = [])`
|
||||||
|
|
||||||
|
`$args` An array which contains the data to insert. Scheme: `['Spalte' => 'Daten']`
|
||||||
|
|
||||||
|
## update()
|
||||||
|
|
||||||
|
Updates already existing data in the database.
|
||||||
|
|
||||||
|
Returns `true` on success, otherwise `false`.
|
||||||
|
|
||||||
|
### Syntax
|
||||||
|
|
||||||
|
`update($where = [], $dataToUpdate = [], $link = 'AND')`
|
||||||
|
|
||||||
|
`$where`
|
||||||
|
An array to update specific data. If provided, only data which matches is updated. Scheme: `['key' => 'value']`.
|
||||||
|
**If not provided, all data is updated!**
|
||||||
|
|
||||||
|
`$dataToUpdate`
|
||||||
|
An array with data to update. Scheme: `['Spalte' => 'Neuer Inhalt']`
|
||||||
|
|
||||||
|
You can also provde the data via `$db->data['key'] = 'value'`.
|
||||||
|
|
||||||
|
`$link`
|
||||||
|
If you provide more than one key, you can choose via this pararmeter how to connect them, currently via `AND` or `OR`. Standard is `AND`.
|
||||||
|
|
||||||
|
## delete()
|
||||||
|
|
||||||
|
Deletes data from the database.
|
||||||
|
|
||||||
|
Returns `true` on success, otherwise `false`.
|
||||||
|
|
||||||
|
### Syntax
|
||||||
|
|
||||||
|
`delete($where = [], $link = 'AND')`
|
||||||
|
|
||||||
|
`$where`
|
||||||
|
An array to update specific data. If provided, only data which matches is deleted. Scheme: `['key' => 'value']`.
|
||||||
|
**If not provided, all data is deleted!**
|
||||||
|
|
||||||
|
`$link`
|
||||||
|
If you provide more than one key, you can choose via this pararmeter how to connect them, currently via `AND` or `OR`. Standard is `AND`.
|
||||||
|
|
||||||
|
## clear()
|
||||||
|
|
||||||
|
Resets the current table and contents of `$db->data`. This function will be executed automatically if you execute `$db->setCol()`.
|
||||||
|
|
||||||
|
# Examples
|
||||||
|
|
||||||
|
## Getting Data
|
||||||
|
|
||||||
|
```php
|
||||||
|
`$db = new db('localhost', 'testdb', 'root', '123456789');`
|
||||||
|
`$db->setCol('blog');`
|
||||||
|
`$db->data['id'] = 3;`
|
||||||
|
`print_r($db->get());`
|
||||||
|
```
|
||||||
|
|
||||||
|
_Or:_
|
||||||
|
|
||||||
|
```php
|
||||||
|
`$db = new db('localhost', 'testdb', 'root', '123456789');`
|
||||||
|
`$db->setCol('blog');`
|
||||||
|
`print_r($db->get(['id' => 3]));`
|
||||||
|
```
|
||||||
|
|
||||||
|
Will return the same data.
|
||||||
|
|
||||||
|
## Insert Data
|
||||||
|
|
||||||
|
```php
|
||||||
|
`$db = new db('localhost', 'testdb', 'root', '123456789');`
|
||||||
|
`$db->setCol('blog');`
|
||||||
|
`$db->data['title'] = 'test';`
|
||||||
|
`$db->data['content'] = 'Lorem Ipsum...';`
|
||||||
|
`if($db->insert()) echo 'Data was inserted';`
|
||||||
|
```
|
||||||
|
|
||||||
|
_Or:_
|
||||||
|
|
||||||
|
```php
|
||||||
|
`$db = new db('localhost', 'testdb', 'root', '123456789');`
|
||||||
|
`$db->setCol('blog');`
|
||||||
|
`if($db->insert(['title' => 'test', 'content' => 'Lorem Ipsum...'])) echo 'Data was inserted';`
|
||||||
|
```
|
||||||
|
|
||||||
|
## Update Data
|
||||||
|
|
||||||
|
```php
|
||||||
|
`$db = new db('localhost', 'testdb', 'root', '123456789');`
|
||||||
|
`$db->setCol('blog');`
|
||||||
|
`$db->data['title'] = 'New';`
|
||||||
|
`$db->data['content'] = 'Lorem';`
|
||||||
|
`$db->update(['id' => 10]);`
|
||||||
|
```
|
||||||
|
|
||||||
|
_Or:_
|
||||||
|
|
||||||
|
```php
|
||||||
|
`$db = new db('localhost', 'testdb', 'root', '123456789');`
|
||||||
|
`$db->setCol('blog');`
|
||||||
|
`if($db->update(['title' => 'New', 'content' => 'Lorem'], ['id' => 10])) echo 'Data was updated successfully.';`
|
||||||
|
```
|
||||||
|
|
||||||
|
## Delete Data
|
||||||
|
|
||||||
|
```php
|
||||||
|
`$db = new db('localhost', 'testdb', 'root', '123456789');`
|
||||||
|
`$db->setCol('blog');`
|
||||||
|
`$db->data['id'] = 9;`
|
||||||
|
`if($db->delete()) echo 'Data was deleted successfully.';`
|
||||||
|
```
|
||||||
|
|
||||||
|
_Or:_
|
||||||
|
|
||||||
|
```php
|
||||||
|
`$db = new db('localhost', 'testdb', 'root', '123456789');`
|
||||||
|
`$db->setCol('blog');`
|
||||||
|
`if($db->delete(['id' => 9])) echo 'Data was deleted successfully.';`
|
||||||
|
```
|
Loading…
Reference in New Issue
Block a user