Monday, July 23, 2007

Cutting Code

catching up with some things I glossed over last week...

Creating a table

once you have a database server and a web server set up on your local machine, you'll need to open a web browser and start up phpMyAdmin. Something like: (the capital letters may or may not be important)

The first time you log in the default administration account for mySQL is root with a blank password. Since this database will only be used for development we can keep this, but if you were setting up a database on an internet facing web server you'd want to set the password (or better, create another user and delete the root user altogether).

Once you're logged in to the database server you'll need to create a database (test) and create a table on it (contacts) with the following fields (as set out in the tutorial):

idINT6A unique identifier for each record
firstVARCHAR15The person's first name
lastVARCHAR15The person's last name
phoneVARCHAR20The person's phone number
mobileVARCHAR20The person's mobile number
faxVARCHAR20The person's fax number
emailVARCHAR30The person's e-mail address
webVARCHAR30The person's web address

Inserting some data

In order for our php interpreter to use data in the database you need to tell it a few things:
  1. where the database server is located (on the development machine that's easy, it's the localhost, but in the real world the database server is usually another computer with an ip address all of its own
  2. Who's trying to connect and the password (to keep out casual snoopers)
  3. The name of the database you're trying to access (a database server can host mutliple databases - each database consisting of a collection of tables and indexes)
Typically this information will be kept in a single file which gets included on all the php pages that access the database. This means you can change the details in just one spot to update the whole site when you come to move it from the development environment to the production server.

I keep this information in a connect.php file which has the following code in it:

@mysql_select_db($database) or die( "Unable to select database");

$user, $password and $database are all variables (php variables all start with a $ sign) which can be changed at any time. You can include it in your php pages by inserting the following code:

include "connect.php"

the actual process of inserting records is outlined quite well in part 3 of the tutorial.

This week we went on with part four looking at how we get new values from a form to insert into the table. We then worked through the rest of that part to see how to display the contents of the table on a web page.

And that was about it for this week - next week we'll look into how to create a search form, and how to sort the results.

Still to come: editing and deleting records, and then: using a multi-table database.

Image: 'Neon scissors'


dayne.mahoney said...

Dayne Mahoney's blog

Jude said...

Hi Bothered by bees,Judy had a blog at

Adam said...

Dont take a wrong turn,just yet. Look both ways when it comes to tafe classes.

Little John said...

This is getting more complicated by the minute. Don't think the grey matter can handle this, may blow a neuron or 2 at any time!!