Create Database In MYSQL Using PHP Script Example

  • 29-07-2022
  • 823
  • PHP
  • Haresh Chauhan

Create Database In MYSQL Using PHP Script Example; In this post, I will create a database in MySQL server with the take help of PHP script. This is very easy and simple to create a database using PHP. At the end of this post, you will find your question's answer on how to create a database in PHPMyAdmin using PHP code.

First we will connect MYSQL server with the PHP script using new mysqli(). In this, we have to pass on three arguments. The first argument is "SERVER NAME", the second argument is "USERNAME", and the Third argument is "PASSWORD". Using the if condition we will check whether the server successfully connected or not.

If the server is successfully connected then we will create a query "CREATE DATABASE {NEW DATABASE NAME}". This query we will run with the given connection. If the query execution return "TRUE" then the database was created successfully else not.

Let's see with an example using a PHP script to create a database in MYSQL.

<?php

$serverName = "localhost";
$username = "root";
$password = "";

// Create connection
$conn = new mysqli($serverName, $username, $password);

// Check connection
if ($conn->connect_error) {

    die("Connection failed: " . $conn->connect_error);
}

// Create database
$sql = "CREATE DATABASE {DATABASE_NAME}";

if ($conn->query($sql) === TRUE) {

    echo "Database created successfully";
} else {

    echo "Error creating database: " . $conn->error;
}

$conn->close();

?>

How to connect database with php


We always thanks to you for reading our blogs.


dharmesh-image

Dharmesh Chauhan

(Swapinfoway Founder)

Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us

haresh-image

Haresh Chauhan

(Co-Founder)


We Are Also Recommending You :