Create database PHP code to write your own BLOG system in PHP

  • 2020-03-31 20:38:08
  • OfStack

Let's go straight to the code

<?php 
//date_default_timezone_set("Asia/Shanghai"); 
 
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(" Delete table siteinfo failure !"); 
mysql_query($sql,$conn)or die(" Create a table siteinfo failure !"); 
} 
 
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(" Database deletion failed !"); 
mysql_query($sql,$conn)or die(" Database creation failed !"); 
} 
 
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(" Database deletion failed !"); 
mysql_query($sql,$conn)or die(" Database creation failed !"); 
} 
 
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(" delete ".$table_class." failure !"); 
mysql_query($sql,$conn)or die(" Create a table ".$table_class." failure "); 
} 
 
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(" Delete table ".$table_guest." failure "); 
mysql_query($sql,$conn)or die(" Create a table ".$table_guest." failure "); 
} 
function create_sql() 
{ 
global $table_article,$table_member,$table_class,$table_guest,$conn; 
echo " create siteinfo table r ... "; 
create_siteinfo(); 
echo " complete <br>"; 
echo " create ".$table_article." table r ... "; 
create_article(); 
echo " complete <br>"; 
echo " create ".$table_member." table r ... "; 
create_member(); 
echo " complete <br>"; 
echo " create ".$table_class." table r ... "; 
create_class(); 
echo " complete <br>"; 
echo " create ".$table_guest." table r ... "; 
create_guest(); 
echo " complete <br>"; 
mysql_close($conn); 
} 
?> 

Related articles: