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 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 Chauhan
(Co-Founder)We Are Also Recommending You :
- Match Function Php 8
- Generate Captcha Code Image With Form Validation - PHP
- Postgres Database Export
- How To Change ENUM DataType Without Losing Data Laravel Migration
- How To Check Given URL Is Valid Or Not PHP Function Example
- Simple WhatsApp Messages Send Using Chat API In PHP
- IOS Push Notification Send Example
- Get Zipcode/Pincode From Latitude and Longitude PHP and Google Maps API
- How To Add Google reCAPTCHA v2 In HTML Form Based PHP/Laravel Website
- How To Add Google reCAPTCHA v3 In HTML Form Based PHP/Laravel Website