PHP develops simple book backend system to create database and tables
First create a database about books, named book.
<?php // 创建连接 $conn = new mysqli("localhost", "uesename", "password"); // 检测连接 if ($conn->connect_error) { die("连接失败: " . $conn->connect_error);} // 创建数据库 $sql = "CREATE DATABASE book"; if ($conn->query($sql) === TRUE) { echo "数据库创建成功"; } else { echo "Error creating database: " . $conn->error; } $conn->close(); ?>
Create an administrator table in the database, named admin
Set the following fields:
id: It is unique, of type int, and selects the primary key.
username: Administrator name, type is varchar, length is 50.
password: Password, type is varchar, length is 50.
<?php $SQL = " CREATE TABLE IF NOT EXISTS `admin` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(50) CHARACTER SET utf8 DEFAULT NULL, `password` varchar(50) CHARACTER SET utf8 DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=2 "; ?>
Here we need to continue to use the yx_book table of "PHP to develop a simple book lending system"
The fields are as follows:
id: It is unique, type is int, and Select the primary key.
name: Book name, type is varchar, length is 20.
price: Price, type decimal(4,2), used for data storage with relatively high precision.
The declaration syntax of decimal column is decimal(m,d).
1. M is the maximum number of numbers (precision). Its range is 1 to 65 (in older MySQL versions, the allowed range was 1 to 254).
2. D is the number of digits to the right of the decimal point (scale). Its range is 0~30, but it must not exceed M.
uploadtime: storage time, type is datetime.
type: Book classification, type is varchar, length is 10.
total: Number of books, type is int, length is 50.
leave_number: The number of remaining books that can be borrowed, type is int, length is 10.
<?php $SQL = " CREATE TABLE IF NOT EXISTS `yx_books` ( `id` int(10) NOT NULL AUTO_INCREMENT, `name` varchar(20) CHARACTER SET utf8 NOT NULL, `price` decimal(4,2) NOT NULL, `uploadtime` datetime NOT NULL, `type` varchar(10) CHARACTER SET utf8 NOT NULL, `total` int(50) DEFAULT NULL, `leave_number` int(10) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=42 "; ?>
Write the created database table into the config.php file so that we can call the database table on different pages in the future.
<?php ob_start(); session_start(); //开启缓存 header("Content-type:text/html;charset=utf-8"); $link = mysqli_connect('localhost','username','password','book'); mysqli_set_charset($link, "utf8"); if (!$link) { die("连接失败:".mysqli_connect_error()); } ?>