In our last article we discussed creating a MySQL database using phpMyAdmin, but what if we want to do it using PHP alone? We can do that. This tutorial will show how.
We will use four PHP functions:
In this example, we will create a database called “movies”. We will assume the MySQL server is on the localhost which it is if you are running WampServer 2 as recommended in our tutorials.
The following code will create our database. Please read the comments to understand what is happening.
<?php // First we will connect to our server. // Indicate the server address, username, and password. // If you have followed our tutorials since the beginning, your username is root. $link = mysql_connect("localhost","root","YOUR-PASSWORD-HERE"); // Now we will check to see if our $link variable shows we connected. if (!$link) // If it did not connect... { die('Sorry, I encountered this error: ' . mysql_error()); // The die function ends the script and uses the mysql_error to show us why we could not connect. } // If we've made it this far, then our connection was successful. // So now we will create our database using the SQL "create database" command. // Using the $link connection, send our request to the server to create our movies database. if (mysql_query("CREATE DATABASE movies",$link)) { // If successful, tell us it worked. echo "Database has been created."; } else // But if it doesn't work, tell us why... { echo "Could not create database: " . mysql_error(); } // Now that we are finished with the connection, we will close it. mysql_close($link); ?>
And there it is. If successful, you will now have an empty database called “movies”. In our next tutorial, we will learn how to create tables in the database.
There are a few good ways to create a MySQL database. One of the easiest (and most visual) methods is to use phpMyAdmin. If you’ve followed our tutorials since the beginning, then you already have phpMyAdmin installed and ready to use.
Like usual in our tutorials, the first step is to make sure your local server is running, so make sure WAMP Server is online. Click on the WAMP icon in your Windows taskbar and choose phpMyAdmin. This will open your Web browser and take you to http://localhost/phpmyadmin/.

WAMP Server Menu
The phpMyAdmin index page will load. It shows you a list of databases in the left column. It also gives you information about your MySQL server, your Web server, and phpMyAdmin itself.
Creating a new MySQL database with this tool is very easy. Near the middle of the screen, you’ll see the option to create a new database. Type in the database name “movies”. Your screen should look like this:

Create Movies Database
The next thing to do is click Create. And then… congrats! You just created your database. It will show in the left column as “movies (0)”. The zero represents the current number of tables, which is none for now.
You might be curious what the “collation” stuff is all about. In short, a collation is a set of rules for comparing characters in a character set. For now, just set it to what you see in the image above, which is utf8_general_ci. Utf8 is one of the most common to use. If you’d like to learn more about collation, please visit MySQL’s official article.
Tags: collation, database, mysql, phpmyadmin