Â
Creating MySQL Database Using PHP
Now that you've understood how to open a connection to the MySQL database server. In this tutorial you will learn how to execute SQL query to create a database.
Before saving or accessing the data, we need to create a database first. The CREATE DATABASE
 statement is used to create a new database in MySQL.
Let's make a SQL query using the CREATE DATABASE
 statement, after that we will execute this SQL query through passing it to the PHP mysqli_query()
 function to finally create our database. The following example creates a database named demo.
Code
<?php /* Attempt MySQL server connection. Assuming you are running MySQL server with default setting (user 'root' with no password) */ $link = mysqli_connect("localhost", "root", ""); // Check connection if($link === false){ die("ERROR: Could not connect. " . mysqli_connect_error()); } // Attempt create database query execution $sql = "CREATE DATABASE demo"; if(mysqli_query($link, $sql)){ echo "Database created successfully"; } else{ echo "ERROR: Could not able to execute $sql. " . mysqli_error($link); } // Close connection mysqli_close($link); ?>
0 Comments