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: http://127.0.0.1/phpmyadmin (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):
Name | Type | Length | Description |
id | INT | 6 | A unique identifier for each record |
first | VARCHAR | 15 | The person's first name |
last | VARCHAR | 15 | The person's last name |
phone | VARCHAR | 20 | The person's phone number |
mobile | VARCHAR | 20 | The person's mobile number |
fax | VARCHAR | 20 | The person's fax number |
email | VARCHAR | 30 | The person's e-mail address |
web | VARCHAR | 30 | The person's web address |
Inserting some dataIn order for our php interpreter to use data in the database you need to tell it a few things:
- 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
- Who's trying to connect and the password (to keep out casual snoopers)
- 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:
<?
$user="root";
$password="";
$database="test";
mysql_connect(localhost,$user,$password);
@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'
www.flickr.com/photos/48889087714@N01/9658510