当前位置:网站首页>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 .
边栏推荐
- 1146_ SiCp learning notes_ exponentiation
- 一入“远程”终不悔,几人欢喜几人愁。| 社区征文
- i++与++i的区别:通俗易懂的讲述他们的区别
- Research Report on market demand and investment planning for the development of China's office chair industry, 2022-2028
- Leetcode Valentine's Day Special - looking for a single dog
- Deops入门
- Hongmeng fourth training
- [Yu Yue education] family education SPOC class 2 reference materials of Shanghai Normal University
- The third day of writing C language by Yabo people
- Research on Swift
猜你喜欢

STM32 realizes 74HC595 control

Codeforces Round #803 (Div. 2) C. 3SUM Closure

Discussion sur la logique de conception et de mise en oeuvre du processus de paiement

Investigation on the operation prospect of the global and Chinese Anti enkephalinase market and analysis report on the investment strategy of the 14th five year plan 2022-2028

Is AI too slow to design pictures and draw illustrations? 3 sets of practical brushes to save you

Research Report on investment trends and development planning of China's thermal insulation material industry, 2022-2028

微服务组件Sentinel控制台调用

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

Wechat applet for the first time

互聯網醫院HIS管理平臺源碼,在線問診,預約掛號 智慧醫院小程序源碼
随机推荐
How to install PHP on Ubuntu 20.04
Research on Swift
Stm32h7 Hal library SPI DMA transmission has been in busy solution
Detailed explanation of common network attacks
Is AI too slow to design pictures and draw illustrations? 3 sets of practical brushes to save you
Wechat applet for the first time
STM32 realizes 74HC595 control
The gbase 8A database does not support the DB2 function value (column_name, 0) cluster syntax
Graduation summary
Research Report on competitive strategy Outlook Analysis and investment strategic planning of China's smart home equipment industry, 2022-2028
MySQL grouping query
Global and Chinese health care OEM and ODM market status survey and investment planning recommendations report 2022-2028
PUT vs. POST for Uploading Files - RESTful API to be Built Using Zend Framework
自动渗透测试工具核心功能简述
鸿蒙第三次培训
Records of long objects and long judgments in the stream of list
AcWing 4489. Longest subsequence
How to read the source code [debug and observe the source code]
(9) Opencv Canny edge detection
ES6类的继承
