Home >Backend Development >PHP Tutorial >How to create a database via PHP

How to create a database via PHP

清浅
清浅Original
2019-01-19 17:35:0612173browse

Method to create a database through PHP: 1. Establish a connection between the PHP script and the MySQL server; 2. Write a SQL query to create the database and store it in a string variable; 3. Execute the query; 4. Close database.

How to create a database via PHP

The operating environment of this article: windows7 system, mysql8.0&&PHP7.1 version, Dell G3 computer.

A database is a collection of interrelated data. We can efficiently retrieve, insert and delete data from the database and organize the data in the form of tables, views, schemas, etc. Today we will introduce how to create a MySQL database through PHP

Basic steps for creating a MySQL database with PHP:

(1) Establish a connection between the PHP script and the MySQL server

(2) If the connection is successful, write a SQL query to create the database and store it in a string variable

(3) Execute the query

(4) Close the database

Next, in the article, I will introduce the process of creating a database in PHP in detail

<?php
 header("Content-Type: text/html; charset=utf8");
$servername = "localhost";
$username = "username"
$password = "password"
//创建链接
$conn = new mysqli($servername,$username,$password);
if($conn->connect_error){
	die("连接失败" .$conn->connect_error);
}
//创建一个为newDemo的数据库
$sql = "CREATE DATABASE newDemo";
//mysqli_query() 函数用于执行某个针对数据库的查询。
if($conn->query($sql) === TRUE){

	echo "数据库创建成功";
}
else {
    echo "Error creating database: " . $conn->error;
}
//关闭数据库
$conn->close();
?>

Rendering:

How to create a database via PHP

How to create a database via PHP

##[Recommended courses: PHP tutorial, MySQL database

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

Statement:
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