Last 15 Snips show! Welcome to Snipped, a code gallery for creating, saving and sharing all kinds of code snippets.

Erstelle MySql Tabellen per PHP Install Script

PHP · April 29, 2016 10:25 pm

0 0 489

Für ein Installations Script in PHP. Einfache Lösung MySql Tabellen automatisch anlegen zu lassen.

<?php
class CreateTableDemo{
const DB_HOST = 'localhost';
const DB_NAME = 'peter';
const DB_USER = 'pan';
const DB_PASSWORD = 'pass';

private $conn = null;

/**
* Open the database connection
*/
public function __construct(){
// open database connection
$connectionString = sprintf("mysql:host=%s;dbname=%s",
CreateTableDemo::DB_HOST,
CreateTableDemo::DB_NAME);
try {
$this->conn = new PDO($connectionString,
CreateTableDemo::DB_USER,
CreateTableDemo::DB_PASSWORD);

} catch (PDOException $pe) {
die($pe->getMessage());
}
}


/**
* create the tasks table
* @return boolean returns true on success or false on failure
*/
public function createTaskTable() {
$sql = "

SET NAMES utf8;
SET time_zone = '+00:00';
SET foreign_key_checks = 0;
SET sql_mode = 'NO_AUTO_VALUE_ON_ZERO';

DROP TABLE IF EXISTS `startseite`;
CREATE TABLE `startseite` (
`id` int(255) NOT NULL,
`titel` varchar(255) NOT NULL,
`description` varchar(500) NOT NULL,
UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `startseite` (`id`, `titel`, `description`) VALUES
(1, 'Instagram CMS System', 'Beschreibung ...');

";

if($this->conn->exec($sql) !== false)
return true;
return false;
}

/**
* close the database connection
*/
public function __destruct() {
// close the database connection
$this->conn = null;
}
}

// create tasks table
$obj = new CreateTableDemo();
if($obj->createTaskTable()){
echo 'Tasks table created successfully';
}else{
echo 'Error occured in creating the Tasks table';
}

Discuss this Snip
    No Discussion Comments found.

    You must be Signed In to discuss this Snip
Chris

Chris

Joined on
April 28, 2016

  • 22 Snips
    Contributed
  • 19 Likes
    Given
  • 8 Comments Posted
Snip Tags
Get Social And Share

Manage and share all of your code snippets in one easy place.