PHP development to create databases and data tables
After introducing the code, let’s talk about the creation of databases and data tables.
For ease of use, the names we define when creating databases and data tables should be as close or identical to the functions as possible.
Field name | id | name | title | content | reply | time |
Field type | int | varchar | varchar | text | text | datetime |
Field length | 10 | 30 | 30 | |||
id number | username | title | content | reply | time |
<?php // 创建连接 $conn = new mysqli("localhost", "root", "root"); // 检测连接 if ($conn->connect_error) { die("连接失败: " . $conn->connect_error);} // 创建数据库 $sql = "CREATE DATABASE message"; if ($conn->query($sql) === TRUE) { echo "数据库创建成功"; } else { echo "Error creating database: " . $conn->error; } $conn->close(); ?>The above is the database creation statement to create a database called message. The next step is to create the data table, including the field names. Of course, the names should be the same as the functions:
<?php $SQL = " CREATE TABLE IF NOT EXISTS `detials` ( `id` int(10) NOT NULL AUTO_INCREMENT, `name` varchar(30) CHARACTER SET utf8 NOT NULL, `title` varchar(30) CHARACTER SET utf8 NOT NULL, `content` text CHARACTER SET utf8 NOT NULL, `reply` text CHARACTER SET utf8 NOT NULL, `time` datetime NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `id` (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 "; ?>In this way, the database is built, and you can connect to the database and operate it. Of course, you can also directly enter the database creation.