From 55922ed5cc7080f6ab39f923eea23cda984abdeb Mon Sep 17 00:00:00 2001 From: kolaente Date: Sun, 31 Jul 2016 19:39:53 +0200 Subject: [PATCH] Added "Create Table"-Function --- db-blank.php | 13 +++++++++++++ db-mysql.php | 26 +++++++++++++++++++++++++- example.php | 3 ++- readme.md | 16 ++++++++++++++++ 4 files changed, 56 insertions(+), 2 deletions(-) diff --git a/db-blank.php b/db-blank.php index 69f728f..04f3219 100755 --- a/db-blank.php +++ b/db-blank.php @@ -127,6 +127,19 @@ class db } } + //Create Table + public function createCol($name, $rows) + { + $dataTypes = ['int' => 'bigint(11) NOT NULL', 'string' => 'text CHARACTER SET utf8 NOT NULL', 'longstring' => 'longtext CHARACTER SET utf8 NOT NULL', 'boolean' => 'tinyint(1) NOT NULL']; + + } + + //Version + public function version() + { + + } + //Aufräumen public function clear() { diff --git a/db-mysql.php b/db-mysql.php index 798c736..214b34b 100755 --- a/db-mysql.php +++ b/db-mysql.php @@ -24,7 +24,14 @@ class db $this->pass = $pass; $this->prefix = $prefix; - $this->dbh = new PDO('mysql:host=' . $host . ';dbname=' . $dbname, $usr, $pass); + try + { + $this->dbh = new PDO('mysql:host=' . $host . ';dbname=' . $dbname, $usr, $pass); + } + catch(PDOException $e) { + echo $e->getMessage(); + exit; + } //UTF-8 $this->dbh->exec("SET NAMES 'utf8'"); @@ -255,6 +262,23 @@ class db } } + //Create Table + public function createCol($name, $rows) + { + $dataTypes = ['int' => 'bigint(11) NOT NULL', 'string' => 'text CHARACTER SET utf8 NOT NULL', 'longstring' => 'longtext CHARACTER SET utf8 NOT NULL', 'boolean' => 'tinyint(1) NOT NULL']; + $stmt = 'CREATE TABLE '.$name.'('; + foreach ($rows as $colname => $coldata) + { + if(array_key_exists($coldata, $dataTypes)) + { + $stmt .= $colname . ' ' . $dataTypes[$coldata] . ','; + } + } + $stmt = substr($stmt, 0, strlen($stmt) - 1); + $stmt .= ')'; + return $this->dbh->exec($stmt); + } + //Version public function version() { diff --git a/example.php b/example.php index 838051c..3eb5f8f 100755 --- a/example.php +++ b/example.php @@ -1,7 +1,7 @@ setCol('blog'); //$db->data['id'] = 13; //$db->get(); @@ -23,4 +23,5 @@ $db->data['inhalt'] = 'gabs noch net';*/ //$db->delete(); //if($db->delete(['id' => 6])) echo 'del'; +var_dump($db->createCol('test', ['id' => 'int', 'title' => 'string', 'content' => 'longstring', 'boolean' => 'boolean'])); $db->clear(); \ No newline at end of file diff --git a/readme.md b/readme.md index db1d372..46a63d1 100755 --- a/readme.md +++ b/readme.md @@ -108,6 +108,22 @@ If you provide more than one key, you can choose via this pararmeter how to conn Resets the current table and contents of `$db->data`. This function will be executed automatically if you execute `$db->setCol()`. +##createCol() + +Creates a new table. + +###Syntax + +`$db->createCol($name, $structure)` + +`$name` +The name of the Table. + +`$structure` +An array containing the structure of the new table. Example: `['id' => 'int', 'title' => 'string', 'content' => 'longstring']`. + +Supported types are `int`, `string`, `longstring` & `boolean`. + # Examples ## Getting Data