As a Web developer, I usually perform mysql database operations from phpmyadmin or use GUI tool like SQLYOG, HeidiSQL or MYSQL Workbench. But it doesn’t mean that the only way to create and drop database is phpmyadmin or any GUI tool.
You can perform any mysql operation using PHP. If you follow wdb24.com regularly you noticed that I normally add “create database” statement in my tutorials. I do this because I don’t want to create any confusion in my readers mind.
Also read:
- Create MYSQL TABLE using PHP ARRAY
- PHP Login and Remember me Script using Cookie
- PHP Try and Catch Tutorial with Example
Create and Drop MYSQL Database using PHP
MYSQL Create Database:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<?php $host = "localhost"; $user = "root"; $password = ""; $db = "demo"; $conn = mysqli_connect("localhost","root",""); if (!$conn) { die("Connection Error : " . mysqli_connect_error()); } $createDatabase = "create database my_blog"; $result = mysqli_query($conn, $createDatabase); if($result) { echo "Database created successfully"; } else { echo "Unable to create database ".mysqli_error($conn); } ?> |
MYSQL Drop Database:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<?php $host = "localhost"; $user = "root"; $password = ""; $conn = mysqli_connect("localhost","root",""); if (!$conn) { die("Connection Error : " . mysqli_connect_error()); } $createDatabase = "drop database my_blog"; $result = mysqli_query($conn, $createDatabase); if($result) { echo "Database created successfully"; } else { echo "Unable to create database ".mysqli_error($conn); } ?> |