PHP MySQL
Create & Test MySQL Database

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 at the Beginning.
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.

 

Syntax

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.15\htdocs folder or create a new folder within the htdocs folder to keep things tidy. Save and run it from there.

Copy & Save as: 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 2014 users:
With server running,
Load birthdays_create_table.php into the editor window
Click Preview.

If there are no error messages, the blank screen 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

MySQL Tutorial

To extend your knowledge of MySQL study the Docs and Tutorials at the official MySQL website. MYSQL.com