当前位置:网站首页>PHP MySQL create database
PHP MySQL create database
2022-07-03 17:50:00 【Crooning ~ shallow singing】
The database holds one or more tables .
You need CREATE Permission to create or delete MySQL database .
Use MySQLi and PDO establish MySQL database
CREATE DATABASE Statement is used in the MySQL Create database .
In the following example , Created a new one called "myDB" The database of :
example (MySQLi - object-oriented )
<?php $servername = "localhost"; $username = "username"; $password = "password"; // Create connection $conn = new mysqli($servername, $username, $password); // Detection connection if ($conn->connect_error) { die(" The connection fails : " . $conn->connect_error); } // Create database $sql = "CREATE DATABASE myDB"; if ($conn->query($sql) === TRUE) { echo " Database created successfully "; } else { echo "Error creating database: " . $conn->error; } $conn->close(); ?>
Be careful : When you create a new database , You have to be mysqli Object specifies three parameters (servername, username and password). Tip: If you use other ports ( The default is 3306), Add an empty string to the database parameter , Such as : new mysqli("localhost", "username", "password", "", port) |
example (MySQLi Procedural)
<?php $servername = "localhost"; $username = "username"; $password = "password"; // Create connection $conn = mysqli_connect($servername, $username, $password); // Detection connection if (!$conn) { die(" The connection fails : " . mysqli_connect_error()); } // Create database $sql = "CREATE DATABASE myDB"; if (mysqli_query($conn, $sql)) { echo " Database created successfully "; } else { echo "Error creating database: " . mysqli_error($conn); } mysqli_close($conn); ?>
Be careful : The following uses PDO Instance create database myDBPDO :
example
Use PDO:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
try {
$conn = new PDO("mysql:host=$servername", $username, $password);
// Set up PDO The error mode is exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "CREATE DATABASE myDBPDO";
// Use exec() , Because no results are returned
$conn->exec($sql);
echo " Database created successfully <br>";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?>
Tips : Use PDO The biggest advantage of is that when there is a problem in the database query process, you can use exception classes to Dealing with problems . If try{ } Exception in code block , The script will stop executing and jump to the first catch(){ } Code block execution code . In the code block captured above, we output SQL Statement and generate an error message .
边栏推荐
- [combinatorics] recursive equation (special solution example 1 Hannover tower complete solution process | special solution example 2 special solution processing when the characteristic root is 1)
- A day's work list of an ordinary programmer
- MinGW compile boost library
- Write a program to process a list container of string type. Find a special value in the container 9.27: and delete it if found. Rewrite the above procedure with deque container.
- The gbase 8A database does not support the DB2 function value (column_name, 0) cluster syntax
- How to read the source code [debug and observe the source code]
- The difference between get and post
- SQL injection database operation foundation
- c# . Net tool ecosystem
- 鸿蒙第四次培训
猜你喜欢
Introduction to SolidWorks gear design software tool geartrax
Research Report on investment trends and development planning of China's thermal insulation material industry, 2022-2028
MySQL grouping query
POM in idea XML graying solution
How to read the source code [debug and observe the source code]
Market demand survey and marketing strategy analysis report of global and Chinese pet milk substitutes 2022-2028
Golang单元测试、Mock测试以及基准测试
[combinatorics] recursive equation (summary of the solution process of recursive equation | homogeneous | double root | non-homogeneous | characteristic root is 1 | exponential form | the bottom is th
STM32实现74HC595控制
Kubernetes resource object introduction and common commands (V) - (NFS & PV & PVC)
随机推荐
ES6类的继承
远程办公工具分享|社区征文
Draw some simple graphics with MFC
Kotlin的協程:上下文
MySQL grouping query
Inheritance of ES6 class
[combinatorics] recursive equation (special solution form | special solution solving method | special solution example)
Market demand survey and marketing strategy analysis report of global and Chinese pet milk substitutes 2022-2028
Global and Chinese health care OEM and ODM market status survey and investment planning recommendations report 2022-2028
Is AI too slow to design pictures and draw illustrations? 3 sets of practical brushes to save you
Getting started with deops
Write a program to process a list container of string type. Find a special value in the container 9.27: and delete it if found. Rewrite the above procedure with deque container.
How to enforce parameters in PowerShell- How do I make parameters mandatory in PowerShell?
Assembly for unloading Loadfrom() loaded assembly - unloading the assembly loaded with assembly LoadFrom()
Graduation summary
List的stream中Long对象与long判等问题记录
Where is the database account used when running SQL tasks in data warehouse tasks configured
Internet hospital his management platform source code, online consultation, appointment registration smart hospital applet source code
1164 Good in C
Wechat applet for the first time