Courses Job Ready Fresher Training AI for Class 7 to 12 Corporate Training Placements Tutorials
Free Learning Resources

IT Tutorials & Interview Prep

Free guides, interview Q&As, and job responsibility breakdowns — curated by industry veterans to help you crack MNC interviews

109+
Tutorial Articles
9
Topic Categories
100%
Free to Read
← Back to PHP

Creating a MySQL Database

PHP Last Updated: Oct 17, 2025

Creating a MySQL Database

We can use the CREATE DATABASE query inside a PHP script to create a new database in MySQL.

There are also two methods by which we can do it.

MySQLi Object-oriented approach

<?php
$servername = "localhost";
$username = "username";
$password = "password";

// Create a connection
$conn = new mysqli($servername, $username, $password);
// Check the connection
if ($conn->connect_error) {
 die("Connection failed: " . $conn->connect_error);
}

// Create a database query
$sql = "CREATE DATABASE cwhDB";
if ($conn->query($sql) === TRUE) {
 echo "Database created successfully";
} else {
 echo "Error creating database: " . $conn->error;
}

$conn->close();
?>

MySQLi Procedural approach

<?php
$servername = "localhost";
$username = "username";
$password = "password";

// Create a connection
$conn = mysqli_connect($servername, $username, $password);
// Check the connection
if (!$conn) {
 die("Connection failed: " . mysqli_connect_error());
}

// Create a database
$sql = "CREATE DATABASE cwhDB";
if (mysqli_query($conn, $sql)) {
 echo "Database created successfully";
} else {
 echo "Error creating database: " . mysqli_error($conn);
}

mysqli_close($conn);
?>

Want to practice these scenarios in a real IT environment? Our 45-Day Internship Program gives you hands-on experience with Active Directory, Ticketing Tools, and Networking.
Join the Internship →