Added "Create Table"-Function

This commit is contained in:
kolaente 2016-07-31 19:39:53 +02:00
parent 7b742d0386
commit 55922ed5cc
4 changed files with 56 additions and 2 deletions

View File

@ -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()
{

View File

@ -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()
{

View File

@ -1,7 +1,7 @@
<?php
require_once 'db-mysql.php';
$db = new db('localhost', 'testdb', 'root', 'supersecretpassword');
$db = new db('localhost', 'testdb', 'root', 'jup2000');
$db->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();

View File

@ -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