PHP MySQL creates database

PHP MySQL Create database


The database has one or more tables.

You need CREATE permission to create or delete a MySQL database.


Creating a MySQL database using MySQLi and PDO

The CREATE DATABASE statement is used to create a database in MySQL.

In the following example, a database named "myDB" is created:

Instance (MySQLi - Object-oriented)

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


Note: When you create a new database, you must specify three parameters (servername, username and password) for the mysqli object.

Tip: If you use another port (default is 3306), add an empty string for the database parameter, such as: new mysqli("localhost", "username", "password", "", port)

Instance (MySQLi Procedural)

CREATE DATABASE db_name;

statement is used to create a database, db_name is the name we give to the data:

•           It is allowed to use Chinese characters as the name of the database (table names, field names as well)

•    Try not to use reserved words (keywords) in database names

•      Do not use special symbols in the database name, but you can Multiple words are separated by underscores

Each SQL statement will only be executed after entering a semicolon (;) and pressing Enter

<?php
$servername = "localhost"; 
$username = "username"; 
$password = "password";  
// 创建连接 
$conn = mysqli_connect($servername, $username, $password); 
// 检测连接 
if (!$conn) {     die("连接失败: " . mysqli_connect_error()); }  
// 创建数据库 
$sql = "CREATE DATABASE myDB"; 
if (mysqli_query($conn, $sql)) {
     echo "数据库创建成功"; 
     } else {     
     echo "Error creating database: " . mysqli_error($conn); }
       mysqli_close($conn); 
       ?>

Note: The following uses a PDO instance to create the database "myDBPDO":

Example

Using PDO:

<?php 
 $servername = "localhost"; 
 $username = "username"; 
 $password = "password"; 
 
 try { 
     $conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password); 
 
     // 设置 PDO 错误模式为异常 
     $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
     $sql = "CREATE DATABASE myDBPDO"; 
 
     // 使用 exec() ,因为没有结果返回 
     $conn->exec($sql); 
 
     echo "数据库创建成功<br>"; 
 } 
 catch(PDOException $e) 
 { 
     echo $sql . "<br>" . $e->getMessage(); 
 } 
 
 $conn = null; 
 ?>

Tips: The biggest benefit of using PDO is that you can use the exception class to handle problems when problems occur during the database query process. If an exception occurs in the try{ } code block, the script will stop execution and jump to the first catch(){ } code block to execute the code. In the code block captured above we output the SQL statement and generate the error message.


Continuing Learning
||
<?php $servername = "localhost"; $username = "username"; $password = "password"; // 创建连接 $conn = new mysqli($servername, $username, $password); // 检测连接 if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } // 创建数据库 $sql = "CREATE DATABASE myDB"; if ($conn->query($sql) === TRUE) { echo "数据库创建成功"; } else { echo "Error creating database: " . $conn->error; } $conn->close(); ?>
submitReset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!