当前位置:网站首页>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 .
边栏推荐
- Design limitations of structure type (struct)
- How to read the source code [debug and observe the source code]
- 解决Zabbix用snmp监控网络流量不准的问题
- Wechat applet for the first time
- As soon as we enter "remote", we will never regret, and several people will be happy and several people will be sad| Community essay solicitation
- c# .net 工具生态
- 1164 Good in C
- IntelliJ 2021.3 short command line when running applications
- Research Report on investment trends and development planning of China's thermal insulation material industry, 2022-2028
- 远程办公工具分享|社区征文
猜你喜欢

Applet setting multi account debugging

Leetcode Valentine's Day Special - looking for a single dog

国内如何购买Google Colab会员

Leetcode 538 converts binary search tree into cumulative tree -- recursive method and iterative method

Hongmeng third training

1146_ SiCp learning notes_ exponentiation

BFS - topology sort

Records of long objects and long judgments in the stream of list

Introduction to SolidWorks gear design software tool geartrax

Play with fancy special effects. This AE super kit is for you
随机推荐
[UE4] brush Arctic pack high quality Arctic terrain pack
Detailed explanation of common network attacks
Loop through JSON object list
Fedora 21 installs lamp host server
Servlet specification Part II
1147_ Makefile learning_ Target files and dependent files in makefile
SSL / bio pour OpenSSL Get FD
Select 3 fcpx plug-ins. Come and see if you like them
How to read the source code [debug and observe the source code]
Hongmeng fourth training
Golang单元测试、Mock测试以及基准测试
Discussion sur la logique de conception et de mise en oeuvre du processus de paiement
1164 Good in C
Implementation of Tetris in C language
Brief introduction to the core functions of automatic penetration testing tool
[set theory] order relation: summary (partial order relation | partial order set | comparable | strictly less than | covering | hasto | total order relation | quasi order relation | partial order rela
数学公式(测试)
MinGW compile boost library
Hongmeng third training
i++与++i的区别:通俗易懂的讲述他们的区别
