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

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

12 Oct 09 PHP Function Tutorials

Just added two new tutorials about creating and using functions in PHP. I’m still working on another one or two tutorials about functions in specific. Functions are so powerful and important in PHP that I could probably write dozens of articles about them, but these first few tutorials will give beginners what they need to know.

Here’s the first two:

1. PHP Function
A function allows you to reuse a block of code throughout your application.

2. PHP More With Functions
Continue learning more about the power of PHP functions.

These are listed in our Building PHP Scripts, along with most of our tutorials.

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

21 Sep 09 New tutorials on PHP loops

Kind of a slow period so far this month, but I have added a few tutorials in the past couple weeks on the subject of PHP loops.

PHP While Loops
If it’s true, the while loop will repeat.

PHP For Loops
For every time a condition is met, the loop repeats.

PHP For Each Loops
For each item in an array, the loop repeats.

Tags: , , , ,