How to create data table in php database

Release: 2023-02-27 19:48:02
Original
2672 people have browsed it

How to create data table in php database

The database is composed of many data tables. The database is equivalent to a room, and the data table is equivalent to the boxes in the room. Let's take a look at creating data in the PHP database. table method.

Recommended:php server

1. Enter login information

Old rule, if you want to operate the database, you must first connect to the database, then we First we need to create the information we want to log in:

$username="root"; $password=""; $servernmae="localhost"; $dbname="ceshi";
Copy after login

2. Connect to the database

Connecting to the database is very simple and can be done in one sentence. We learned it in the previous article, so here we are. No more cumbersome, the example is as follows:

$connect=new mysqli($servernmae,$username,$password,$dbname);
Copy after login

It’s just that the parameter here has an extra $dbname. We just said the meaning of $dbname, and this parameter must be brought.

3. Fill in the sql statement

Let’s first look at an example of the sql statement to create a data table:

$sql="create table test1( idint(7) unsigned not null auto_increment primary key,//主键 usernamevarchar(64) not null,//用户名 ageint(3) not null,//年龄 register_timetimestamp//注册时间 )";
Copy after login

The test1 above is the custom table name, add What is shown in bold is the field in the table. What follows the field is the type of the field. For example, varchar(64) means that the string type is used, which takes up 64 characters. After the type, there is a qualified statement, such as not null. , indicating that null values are not allowed.

The CREATE TABLE statement is used to create a MySQL table.Each table should have a primary key (this column is the "id" column), and the primary key must contain a unique value.

4. Execute the statement.

After creating the new sql statement, we are almost done. We will execute the sql statement and make corresponding judgments on the execution results. If there is an error, we will give corresponding prompts.

$query=$connect->query($sql); if($query){ echo "成功创建数据表!"; } else{ echo $connect->error; }
Copy after login

5. Test.

Open the local apache and mysql services, enter the URL in the browser to access, the results are as follows:

How to create data table in php database

The above is the detailed content of How to create data table in php database. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!