Loading... # PHP MySQL Create Database In this tutorial you will learn how to create a database in MySQL using PHP. ## 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](http://www.bixiaguangnian.com/manual/sql/3389.html "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. ```php <?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); ?> ``` > Tip: Setting the PDO::ATTR\_ERRMODE attribute to PDO::ERRMODE\_EXCEPTION tells PDO to throw exceptions whenever a database error occurs. Last modification:September 13, 2024 © Allow specification reprint Like 如果觉得我的文章对你有用,请随意赞赏