---
title: "How to create database in MySQL using PHP?"  
description: "The CREATE DATABASE statement is used to create database. (Note: Before creating database you must first connect with database server).Here is an exam"  
author: "Royce Roy"  
published: 2016-06-13  
updated: 2018-03-15  
canonical: https://www.mindstick.com/blog/11152/how-to-create-database-in-mysql-using-php  
category: "php"  
tags: ["database", "php", "mysql"]  
reading_time: 1 minute  

---

# How to create database in MySQL using PHP?

The **[CREATE DATABASE](https://www.mindstick.com/articles/1623/sfdc-create-an-app-and-database)** statement is used to create database. (Note: Before creating database you [must](https://www.mindstick.com/articles/12978/top-reasons-why-every-magento-ecommerce-store-must-opt-for-mobile-app) first [connect with database](https://www.mindstick.com/forum/34348/in-c-sharp-how-to-connect-with-database-and-looping-over-a-recordset) [server](https://www.mindstick.com/articles/43769/what-is-serverless-architecture-is-it-worth-switching-over)).Here is an example to create database using [PHP](https://www.mindstick.com/articles/12149/php-constant-and-php-variables):

```
<?php $server_name = "localhost";$uname = "username";$pass= "password";$connection = new mysqli($server_name, $uname, $pass); // connecting with database server
// Check connectionif ($connection->connect_error){    die("Connection failed: " . $connection->connect_error);}// Create databaseif ($connection->query("CREATE DATABASE first_db") === TRUE) {
    echo "Database created successfully"; }else {    echo "Error creating database: " . $connection->error;
 }$connection->close(); // It will close the connection
?>
```

---

Original Source: https://www.mindstick.com/blog/11152/how-to-create-database-in-mysql-using-php

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
