包含五個核心資料表:站點資訊、文章、會員、分類、留言板。每個函式負責建立對應資料表,先 DROP IF EXISTS 再 CREATE。
<?php
function create_siteinfo() {
global $conn;
$sql = "create table siteinfo (
title varchar(100) not null,
keyword varchar(200) not null,
description varchar(200) not null
)";
$dropsql = "drop table if exists siteinfo";
mysql_query($dropsql, $conn) or die("刪除 siteinfo 失敗!");
mysql_query($sql, $conn) or die("建立 siteinfo 失敗!");
}
function create_article() {
global $conn, $table_article;
$sql = "create table $table_article (
id int(11) auto_increment not null,
cid int(5) not null,
abstract varchar(300) not null,
title varchar(50) not null,
posttime datetime not null,
author varchar(30) not null,
comefrom varchar(50) not null,
comeurl varchar(50) not null,
content TEXT not null,
keyword varchar(20) not null,
rank int(2) not null,
views int(5) not null,
PRIMARY KEY(id)
)";
$dropsql = "drop table if exists $table_article";
mysql_query($dropsql, $conn) or die("刪除失敗!");
mysql_query($sql, $conn) or die("建立失敗!");
}
function create_member() {
global $conn, $table_member;
$sql = "create table $table_member (
uid int(5) auto_increment not null,
u_name varchar(20) not null UNIQUE,
u_passwd varchar(100) not null,
rank int(2) not null,
PRIMARY KEY(uid)
)";
$dropsql = "drop table if exists $table_member";
mysql_query($dropsql, $conn) or die("刪除失敗!");
mysql_query($sql, $conn) or die("建立失敗!");
}
function create_class() {
global $conn, $table_class;
$sql = "create table $table_class (
cid int(5) auto_increment not null,
cname varchar(50) not null UNIQUE,
PRIMARY KEY(cid)
)";
$dropsql = "drop table if exists $table_class";
mysql_query($dropsql, $conn) or die("刪除 $table_class 失敗!");
mysql_query($sql, $conn) or die("建立 $table_class 失敗");
}
function create_guest() {
global $conn, $table_guest;
$sql = "create table $table_guest (
gid int(11) auto_increment not null,
g_name varchar(50) not null,
g_site varchar(50) not null,
g_mail varchar(50) not null,
g_cid int(5) not null,
PRIMARY KEY(gid)
)";
$dropsql = "drop table if exists $table_guest";
mysql_query($dropsql, $conn) or die("刪除 $table_guest 失敗");
mysql_query($sql, $conn) or die("建立 $table_guest 失敗");
}
function create_sql() {
global $table_article, $table_member, $table_class, $table_guest, $conn;
echo "建立 siteinfo 表……"; create_siteinfo(); echo "完成<br>";
echo "建立 $table_article 表……"; create_article(); echo "完成<br>";
echo "建立 $table_member 表……"; create_member(); echo "完成<br>";
echo "建立 $table_class 表……"; create_class(); echo "完成<br>";
echo "建立 $table_guest 表……"; create_guest(); echo "完成<br>";
mysql_close($conn);
}
?>
mysql_* 函數,實際專案請改用 mysqli 或 PDO。