PHP MySQL
Interactive Website Design
Creating a Table
A database can contain a multiple number of tables which are arranged in columns and rows. Each table is supplied with a unique name upon creation.
Fields or columns contained within the table are supplied with definitions for type and length.
Note: If you have accessed this web page via a search engine, you should go back and start on our home page. This tutorial is designed to be viewed and executed in sequence. Learn to build your database right on your PC and Export it to your website.
CREATE TABLE friends( id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
firstname VARCHAR(30),
birthday VARCHAR(20));
In the example shown above, we create a table named friends.
The table contains 3 columns:
a primary key field called id which will be automatically incremented each time a record is added.
a
firstname field which will contain a variable number of characters up to 30
a birthday field which will contain a variable number of characters up to 20.
IndigoAMPP Users
This is the actual script found in the birthdays_db download.
It adds a table with 4 columns or fields: id, firstname, lastname and birthday.
You can copy the code right from the page and save it as birthdays_create_table.php or you can save time by downloading the birthdays_db zip file. (See Instructions below)
If you copy and paste, save it in your C:\indigoampp\apache-2.2.11\htdocs folder or create a new folder within the htdocs folder to keep things tidy. Save and run it from there.
birthdays_create_table.php
<?
$db="newdb";
$link = mysql_connect("localhost", "root", "");
if (! $link) die("Couldn't connect to MySQL");
mysql_select_db($db , $link) or die("Select DB Error: ".mysql_error());
/* create table */
mysql_query(
"CREATE TABLE birthdays(
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
firstname VARCHAR(30),
lastname VARCHAR(30),
birthday VARCHAR(20))") or die(mysql_error());
mysql_close($link);
?>
In the example code shown above, before the table can be created, the database must first be selected using the code:
mysql_select_db($db , $link)
Running the Script
Running this script will create a table named birthdays in the newdb database.
HTMLPad 2010 users:
With server running,
Load birthdays_create_table.php into the editor window
Click Preview.
If there are no error messages, it means the table was created successfully.
If you want to verify on the PHPMyAdmin panel, the databases listed in the left column of the entry page will show newdb(1).
If you haven't closed the panel, refresh the PHPMyAdmin page. (right click reload or refresh.)
If you see newdb(1) you are ready to Add Some Records
Building Your Database on Your PC
In order to build your database on your PC and later Export it to your website requires setting up a localhost server on your PC and finding an HTML editor that will sync up with the server.
If you use Windows, the process of setting up and running a localhost so that you can test and edit scripts and build databases is very easy.
I recommend the IndigoAMPP web server and the HTMLPad 2010 HTML editor.
I recently installed IndigoAMPP for Windows on my Vista system and it ran on the first try after installation. Setting up HTMLPad 2010 to work with it is also an easy process.
I created a little tutorial that shows the whole process including how to start the server after you install it. Go to Tutorial
Note: The server is FREE and you can try the HTML editor for 30 sessions or 30 days free. If you can't learn to build your database in that amount of time, don't buy the editor. NO RISK!!
Download the Scripts
The Birthdays Database management files can be downloaded in a zip file.
If using IndigoAMPP download to c:\indigoampp\apache-2.2.11\htdocs.
Extract there and you'll have a birthdays folder inside your htdocs folder. Run the scripts from there.
The package contains an integrated db management system, with a simple interface.
This Instruction file is included in the download.
Download birthdays_db.zip
MySQL Tutorial
To extend your knowledge of MySQL study the Docs and Tutorials at the official MySQL website. MYSQL.com
Contents
Introduction to PHP MySQL
Connecting to the Server
Creating a Database
Adding Records
Displaying Records
Editing Records
Delete Records
Altering Tables
Export Table
Advanced
Multiple Selection Forms
Create a Simple Poll
Need Web Hosting?
We Recommend these products
Free Flash Websites
