Field name | Field type | Field length | Field description |
id | int | #11 | |
#title | varchar | 30 | Article title |
author | varchar | 30 | Article author |
description | text |
| ##Article description |
content | text |
| ##Article content |
dateline | int11 | | Published time |
Database creation
We run mysql under the command prompt window (specifically how to connect through the command prompt window Database, you can refer to Section 2.2 in our previous course "PHP Development Login Registration Tutorial")
After successfully connecting to the database, copy the complete statement to create the database below into the window, and press Press Enter to prompt that the creation is successful, as shown below
Complete statement to create the database
DROP DATABASE IF EXISTS articledb;
CREATE DATABASE articledb DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
USE articledb;
CREATE TABLE article(
id int(11) NOT NULL AUTO_INCREMENT,
title varchar(30) NOT NULL,
author varchar(30) DEFAULT NULL,
description text DEFAULT NULL,
content text DEFAULT NULL,
dateline int(11) DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
insert into article(title,author) values('admin','admin');
Statement explanation
First determine whether the articledb database exists, and if it exists, delete it first
Create the articledb database after judgment, encode it in utf8 format
Select the articledb database we created
Create article data table
The table contains 6 fields, of which id is the primary key and grows by itself
Insert a piece of data after creation , which is convenient for us to use later
##Database configuration file information
config.php
<?php
header("Content-type:text/html;charset=utf-8");
define('HOST','127.0.0.1');
define('USERNAME','root');
define('PASSWORD','root');
?>
Code explanation:
Store the database login information in constant form Up, this is convenient for our calls and future modifications header("Content-type:text/html;charset=utf-8"); This sentence must exist, which defines the encoding format. If not, The Chinese characters displayed on the page will be garbled
##Connection database information
We will separate the statement to connect to the database, so that when connecting to the database later, just call it directly
The connect.php code is as follows<?php
require_once('config.php');
$conn = mysqli_connect(HOST,USERNAME,PASSWORD);//数据库帐号密码为安装数据库时设置
if(mysqli_errno($conn)){
echo mysqli_errno($conn);
exit;
}
mysqli_select_db($conn,"articledb");
mysqli_set_charset($conn,'utf8');
?>
Code explanation:
Introduced the database configuration file
Connecting to the database failed and prompted an error message Select the database we just created The device encoding format is utf8- Next Section
DROP DATABASE IF EXISTS articledb;
CREATE DATABASE articledb DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
USE articledb;
CREATE TABLE article(
id int(11) NOT NULL AUTO_INCREMENT,
title varchar(30) NOT NULL,
author varchar(30) DEFAULT NULL,
description text DEFAULT NULL,
content text DEFAULT NULL,
dateline int(11) DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
insert into article(title,author) values('admin','admin');