19 Jan 10 Create a New MySQL Database with PHP

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:

  1. mysql_connect. This opens a connection to the MySQL server.
  2. mysql_error. If there is a problem, this gives us the error message.
  3. mysq_query. This sends (queries) MySQL information to the server.
  4. mysql_close. This closes your connection to the server.

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.

Tags: , , , ,

18 Nov 09 Create a New MySQL Database with phpMyAdmin

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/.

wampserver-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 Image

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: , , ,

27 Oct 09 PHP & MySQL Database Tutorial: Introduction

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: , , ,

04 Oct 09 New PHP troubleshooting section open

I just finished the first article for our PHP Debugging and Troubleshooting section. It is titled Common Beginner PHP Mistakes. Check it out!

I’ve also added the PHP Continue and PHP Break tutorials to the Building PHP Scripts section.

Next I’ll write some tutorials about functions. Then we’ll probably move onto MySQL stuff and start building some basic applications.

Tags: , , , ,

25 Aug 09 What do I need to use PHP and MySQL?

I’ll do my best to explain as I go, but before learning PHP there are some things you should have:

  • You’ll need to know how to work with files and folders on your computer. Able to create, open, copy, move and save files to locations you choose.
  • You’ll need a relatively new operating system. If you purchased your computer within the last five years you should be fine, but if you’re still using Windows 95 then we might have a problem.
  • You’ll probably need a basic knowledge of HTML. You don’t need to be a master, but PHP is dynamically outputting HTML so you will need to know it. You may be able to figure it out as you go. If so, I recommend you have a HTML web site or book open as a reference if you get stuck.

That’s all you need before continuing. You don’t need to know anything yet about computer programming. I’ll teach you.

To create and use PHP files, we also need the items below. We’ll talk about how to get and install them for free.

Web Server
A web server is what sends the web site data to your web browser for you to see. Apache is the most popular and what we’ll be using in the tutorial.

PHP
This is the language you are learning, where all of the magic happens.

Database
We’re going to be using the MySQL database system, one of the most popular and widely used free databases.

Code Editor
You need something to write your programming code in. You could use basic software like Notepad, but I will introduce you to free software that will make your programming life easier.

Web Browser
If you’re reading this page online, then you’re already using one. Chances are, you’re using Internet Explorer, Mozilla Firefox, or Safari. Most web browsers will work fine.

Tags: , , , ,