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
Introduction
We use databases to store information and the most popular database used with PHP is MySQL. It’s also free, which is very convenient for us! If you’d like to build a site like Facebook, or store your customer’s order history, or keep an inventory of car parts, then you will need to use a database.
What is MySQL?
MySQL is an open-source relational database system. It is very powerful and chances are that you’ve been to many web sites that were using a MySQL database to show information to you. In fact, MySQL stores all the information on this blog.
Databases consist of tables, which consists of columns and rows of information. Here’s an example of a table:
Table Name: car_parts_inventory
| Row # | Part Name | Part ID |
| 1 | Radio | F5JG7 |
| 2 | Muffler | GG446 |
So there’s a very small table that stores information about car parts and their associated ID number. Some large databases may contain hundreds of thousands of rows.
I mentioned earlier that MySQL is a relational database, which basically means that two or more tables can share the same attribute. Let’s do a small example. In our database, we’ll store movie information and use a relation to organize which actors are in the movie.
First we have the table that stores our actors:
Table Name: actors
| actor_id | actor_name |
| 1 | Will Smith |
| 2 | Kevin Spacey |
And here’s the table that stores our movie information:
Table Name: movies
| movie_id | movie_name | movie_star_actor |
| 1 | Independence Day | 1 |
| 2 | The Pursuit of Happyness | 1 |
| 3 | Superman Returns | 2 |
| 4 | Hancock | 1 |
| 5 | Pay It Forward | 2 |
In our movie table we have a unique movie_id for each movie, then the movie’s name, and then a number that represents the actor_id (from the first table) who stars in the movie.
In our next tutorial, we’ll look at how to create our new database in MySQL.
Tags: introduction, mysql, php, tutorial