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

23 Oct 09 Windows 7 is out, new tutorial, and site restructure

Windows 7 Released
Windows 7 was officially released yesterday and was the buzz on many blogs. I didn’t realize that people had operating system launch parties but hey, some people need any excuse to drink! Besides, that Windows cake looks tasty.

I’ll be upgrading soon, just because I like to have the latest version. I stuck with XP for years (after sticking with Windows ME for years, wow!) and switched to Vista with my new PC a few months ago. People are hopeful about Windows 7 — hopefully it works out better than the Vista release did.

New Tutorial
I added one new tutorial this week: PHP Function Return Values.

Site Restructure
In Wordpress, you have Posts and Pages. Posts are used for blogging and Pages are used for static content, like an About or Contact page. I’ve been using the Pages to post my tutorials, but I thought about it and decided I’ll switch to using the Posts for them.

This will allow new tutorials to show up in the blog feed instead of requiring me to make a special post about the new pages on the site. I don’t know why I didn’t think about this to begin with, but it should be better for all you readers out there!

Tags: , ,

23 Oct 09 PHP Programming Tutorial: Function Return Values

In our last two tutorials, we’ve learned how to create our own functions and some things we can do with them. Another important thing to learn about PHP functions is how to return values. If you send a value from your function to your script, you use the return statement. In every function you create, it’s a good habit to return a value, even if this value is only a boolean (true or false). When you return a value, it sends the value back to the script and ends the function.

The most common example of return values is to show a function using the sum of two numbers, so I’ll do that here:

<?php
// This function will add two numbers and return the sum.
function add_numbers($number1,$number2) {
    $total = $number1 + $number2; // add two numbers, assign to $total variable
    return $total; // here is our return value!
}
?>

A return statement can only return one value. It is possible to return multiple values by using an array, but that is something we’ll cover another time.

So now that we’ve created our function, let’s use it!

<?php
// This function will add two numbers and return the sum.
function add_numbers($number1,$number2) {
    $total = $number1 + $number2; // add two numbers, assign to $total variable
    return $total; // here is our return value!
}
 
echo add_numbers(1,3); // This will output 4 because 1 + 3 = 4.
?>

And here’s another example with an If Statement:

<?php
// This function will add two numbers and return the sum.
function add_numbers($number1,$number2) {
    $total = $number1 + $number2; // add two numbers, assign to $total variable
    return $total; // here is our return value!
}
 
add_numbers(5,14);
 
if ($total > 10) {
    echo "Our total is greater than 10."; // Tell user the sum is greater than 10.
} else { 
    echo "Our total is less than 10."; // Tell user the sum is less than 10.
}
?>

Go back to Building PHP Scripts.