Home > Backend Development > PHP Tutorial > How to create a MySQL database in Linux environment through PHP script

How to create a MySQL database in Linux environment through PHP script

王林
Release: 2023-10-05 08:36:01
Original
758 people have browsed it

How to create a MySQL database in Linux environment through PHP script

How to create a MySQL database in a Linux environment through PHP scripts

MySQL is a commonly used relational database management system that can run on a variety of operating systems. Including Linux. In the Linux environment, we can use PHP scripts to create MySQL databases. This article will introduce how to use PHP to write scripts to create a MySQL database and provide specific code examples.

Before you begin, make sure you have installed PHP and MySQL in your Linux environment, as well as the PHP extension to MySQL.

First, we need to use the MySQL extension provided by PHP to connect to the MySQL server. We can use both mysqli or PDO extensions. In this article, we will use the mysqli extension to connect to the MySQL server.

Here is a sample code showing how to connect to a MySQL server:

<?php
$servername = "localhost";
$username = "your_username";
$password = "your_password";

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

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

$conn->close();
?>
Copy after login

In the above code, you need to replace your_username and your_password Replace with your MySQL username and password.

Next, we need to use a PHP script to create the database. Here is a sample code that shows how to create a database named my_database:

<?php
$servername = "localhost";
$username = "your_username";
$password = "your_password";
$dbname = "my_database";

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

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

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

$conn->close();
?>
Copy after login

In the above code, you need to replace your_username and your_password Replace with your MySQL username and password, and my_database with the name of the database you want to create.

After running the above PHP script, if everything goes well, you will see a success message. You can verify whether the database was created successfully by accessing the MySQL server.

Summary:
There are two key steps to create a MySQL database in a Linux environment through PHP scripts: connecting to the MySQL server and creating the database. We can use the mysqli extension to connect to the MySQL server and use PHP scripts to perform operations to create the database. In practical applications, you can customize code to create and manage MySQL databases according to your own needs.

The above is the detailed content of How to create a MySQL database in Linux environment through PHP script. For more information, please follow other related articles on the PHP Chinese website!

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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template