PHP develops a simple news release system to create databases and tables

We can create a database through phpmyadmin. First create a database named test.

Create a data table under this database named new.

The next step is to create various fields under the news table.

What is a field?

In layman’s terms, it is a general term for a class of things.

" id ": The field " id " is a default preferred field. The content under other fields can be repeated, but this field is an Arabic numeral starting from 1. . When setting this field, set the primary key, index, unique and auto-increment. This auto-increment means automatic increase. When any field adds content, this field will automatically increase by 1. That is to say, any field corresponds to a unique id, such as 1, 2, 0 27...

Let's talk about the establishment of news part fields.

1. id: The meaning is the number of each news, it is unique, the type is int, select auto-increment in "Extra", and select the primary key.

2. author: The meaning is the author (news publisher), set the type to varchar and the length is 20

3. title: The meaning It is the news title, the type is varchar, the length is 100, right

4. Content: The meaning is the content of the news, the type is text. Although the text type field also belongs to a character type, its size cannot be specified. If the length is set, the system will prompt an SQL statement error.

5. created_at: The meaning is the publishing time, the type is datetime, the length does not need to be set

In this way, the new data table is created.


You can also create database tables through PHP code

First create a test database :

<?php
// 创建连接
$conn = new mysqli("localhost", "uesename", "password");
// 检测连接
if ($conn->connect_error) 
{    
    die("连接失败: " . $conn->connect_error);} 
    // 创建数据库
    $sql = "CREATE DATABASE test";
        if ($conn->query($sql) === TRUE) 
        {    
            echo "数据库创建成功";
        } else {    
            echo "Error creating database: " . $conn->error;
        }
    $conn->close();
?>

Then create a new table in the test database:

<?php
    // 创建连接
    $conn = new mysqli("localhost", "uesename", "password","test");
    // 检测连接
    if ($conn->connect_error) 
    {    
    die("连接失败: " . $conn->connect_error);
    } 
    // 使用 sql 创建数据表
    $sql = "CREATE TABLE new (
    id int(10) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
     title varchar(100) NOT NULL,
      author varchar(20) NOT NULL,
      content text NOT NULL,
      created_at datetime NOT NULL,
    )ENGINE=InnoDB DEFAULT CHARSET=utf8 ";
    if ($conn->query($sql) === TRUE) 
    {    
       echo "Table MyGuests created successfully";
    } else {    
       echo "创建数据表错误: " . $conn->error;
    }
    $conn->close(); 
?>


Continuing Learning
||
<?php // 创建连接 $conn = new mysqli("localhost", "uesename", "password","test"); // 检测连接 if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } // 使用 sql 创建数据表 $sql = "CREATE TABLE new ( id int(10) UNSIGNED AUTO_INCREMENT PRIMARY KEY, title varchar(100) NOT NULL, author varchar(20) NOT NULL, content text NOT NULL, created_at datetime NOT NULL, )ENGINE=InnoDB DEFAULT CHARSET=utf8 "; if ($conn->query($sql) === TRUE) { echo "Table MyGuests created successfully"; } else { echo "创建数据表错误: " . $conn->error; } $conn->close(); ?>
submitReset Code