数据库自动创建

数据库自动创建

完全可以模仿wordpress来写我们的config.php文件,将数据库、主机、账号、密码定义成常量,比如

 

数据库常量定义
 1 // ** MySQL settings - You can get this info from your web host ** //
 2 /** The name of the database for WordPress */
 3 define('DB_NAME', 'contact');
 4 
 5 /** MySQL database username */
 6 define('DB_USER', 'root');
 7 
 8 /** MySQL database password */
 9 define('DB_PASSWORD', '123456');
10 
11 /** MySQL hostname */
12 define('DB_HOST', 'localhost');
13 
14 /** Database Charset to use in creating database tables. */
15 define('DB_CHARSET', 'utf8');
16 
17 /** The Database Collate type. Don't change this if in doubt. */
18 define('DB_COLLATE', '');

 

 

 然后增加了一些自动创建数据库的语句

 

数据库安装
 1 @$conn=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("无法连接mysql数据库服务器");
 2 if (!$conn)return;
 3 
 4 $sql="use ".DB_NAME;
 5 if (!mysql_query($sql)){
 6     $sql="create database ".DB_NAME;
 7     if (!mysql_query($sql)){
 8         echo "无法创建数据库:".DB_NAME;
 9         return;
10     }
11 }
12 
13 @$result=mysql_select_db(DB_NAME,$conn) or die("无法打开数据库:".DB_NAME);
14 if (!$result)return;
15 
16 mysql_query("set names '".DB_CHARSET."'");
17 
18 if (!check_table_exist("c_users")){
19     $sql = 'CREATE TABLE `c_users` ( '
20             . ' `u_id` bigint(20) NOT NULL AUTO_INCREMENT, '
21             . ' `u_email` varchar(60) COLLATE utf8_bin NOT NULL DEFAULT \'""\', '
22             . ' `u_pass` varchar(60) COLLATE utf8_bin NOT NULL DEFAULT \'""\', '
23             . ' `u_name` varchar(250) COLLATE utf8_bin DEFAULT \'""\', '
24             . ' `u_telephone` varchar(20) COLLATE utf8_bin DEFAULT \'""\', '
25             . ' `u_description` longtext COLLATE utf8_bin, '
26             . ' PRIMARY KEY (`u_id`) '
27             . ' ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COLLATE=utf8_bin '
28             . ' ';
29     if (!mysql_query($sql)){
30         echo "无法创建表:c_users";
31         return;
32     }    
33 }
34 
35 if (!check_table_exist("c_contacts")){
36     $sql = 'CREATE TABLE `c_contacts` ( '
37             . ' `c_id` bigint(20) NOT NULL AUTO_INCREMENT, '
38             . ' `u_id` bigint(20) NOT NULL, '
39             . ' `c_email` varchar(60) COLLATE utf8_bin DEFAULT \'""\', '
40             . ' `c_name` varchar(250) COLLATE utf8_bin DEFAULT \'""\', '
41             . ' `c_telephone` varchar(20) COLLATE utf8_bin DEFAULT \'""\', '
42             . ' `c_description` longtext COLLATE utf8_bin, '
43             . ' PRIMARY KEY (`c_id`) '
44             . ' ) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8 COLLATE=utf8_bin ';
45     if (!mysql_query($sql)){
46         echo "无法创建表:c_contacts";
47         return;
48     }    
49 }
50 
51 function check_table_exist($find_table){
52     $row=mysql_query("show tables;");
53     $tables=array();
54     while ($result=mysql_fetch_array($row,MYSQL_ASSOC))
55     {
56         $tables[]=$result['Tables_in_contact'];
57     }
58     unset($result,$row);
59     return (in_array($find_table,$tables));
60 }

 

 

 


 

posted @ 2012-06-21 11:32  伪coder  阅读(272)  评论(0)    收藏  举报