When we create a database, we must first prepare the project rules, such as: which functions need to be developed, which tables and fields are included in the database tables, etc.
First we create a database called news:
<?php // 创建连接 $conn = new mysqli("localhost", "root", "root"); // 检测连接 if ($conn->connect_error) { die("连接失败: " . $conn->connect_error);} // 创建数据库 $sql = "CREATE DATABASE news"; if ($conn->query($sql) === TRUE) { echo "数据库创建成功"; } else { echo "Error creating database: " . $conn->error; } $conn->close(); ?>
Then we create a table and roughly clarify the fields we need:
Field name | id | title | content | author | created_at | updated_at |
Field type | int | varchar | text | varchar | datetime | datetime |
Field length | 10 | 200 | 50 | |||
Field description | id number | Title | Content | Author | Publish time | Update time |
According to the above table, we will write Just create the table statement:
<?php $SQL = " CREATE TABLE IF NOT EXISTS `detials` ( `id` int(10) NOT NULL AUTO_INCREMENT, `title` varchar(200) CHARACTER SET utf8 NOT NULL, `content` text CHARACTER SET utf8 NOT NULL, `author` varchar(50) CHARACTER SET utf8 NOT NULL, `created_at` datetime CHARACTER SET utf8 NOT NULL, `updated_at` datetime NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `id` (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 "; ?>
Okay, now our database and data table are created, and then we can implement our functions.