Table of Contents
How to create a MySQL table using PHP?
Prerequisites:
Code:
Practical case:
Home Backend Development PHP Tutorial How to create a MySQL table using PHP?

How to create a MySQL table using PHP?

Jun 04, 2024 pm 01:57 PM
mysql php

Creating a MySQL table using PHP requires the following steps: Connect to the database. Create the database if it does not exist. Select a database. Create table. Execute the query. Close the connection.

如何使用 PHP 创建 MySQL 表?

How to create a MySQL table using PHP?

Creating a MySQL table using PHP is a simple process, which involves the following steps:

Prerequisites:

  • PHP is installed and configured
  • MySQL database server
  • MySQL database user and password

Code:

<?php
// 连接到 MySQL 数据库
$servername = "localhost";
$username = "root";
$password = "mypassword";
$dbname = "myDB";

// 创建连接
$conn = new mysqli($servername, $username, $password);

// 检查连接
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
} 

// 创建数据库(如果不存在)
$sql = "CREATE DATABASE IF NOT EXISTS $dbname";
if ($conn->query($sql) === TRUE) {
    echo "数据库创建成功";
} else {
    echo "数据库创建失败: " . $conn->error;
}

// 选择数据库
$conn->select_db($dbname);

// 创建表
$sql = "CREATE TABLE IF NOT EXISTS users (
    id INT NOT NULL AUTO_INCREMENT,
    username VARCHAR(255) NOT NULL,
    email VARCHAR(255) NOT NULL,
    PRIMARY KEY (id)
)";

// 执行查询
if ($conn->query($sql) === TRUE) {
    echo "表创建成功";
} else {
    echo "表创建失败: " . $conn->error;
}

// 关闭连接
$conn->close();
?>
Copy after login

Practical case:

Suppose you want to create a A table named "users" with columns "id", "username", and "email". You can use the following code:

// 连接到 MySQL 数据库
$conn = new mysqli($servername, $username, $password, $dbname);

// 创建表
$sql = "CREATE TABLE IF NOT EXISTS users (
    id INT NOT NULL AUTO_INCREMENT,
    username VARCHAR(255) NOT NULL,
    email VARCHAR(255) NOT NULL,
    PRIMARY KEY (id)
)";

// 执行查询
if ($conn->query($sql) === TRUE) {
    echo "表创建成功";
} else {
    echo "表创建失败: " . $conn->error;
}

// 关闭连接
$conn->close();
Copy after login

The above is the detailed content of How to create a MySQL table using PHP?. For more information, please follow other related articles on the PHP Chinese website!

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

How to fix mysql_native_password not loaded errors on MySQL 8.4

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles