Für ein Installations Script in PHP. Einfache Lösung MySql Tabellen automatisch anlegen zu lassen.
[code lang=“php“]
< ?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‘;
}
[/code]