You have your database area created, but that is just giving a name to the area. It is a big blank space. The next step is to put at least one table in it.

Tables are the main part of a database. A database will hold one to many tables depending on what you are trying to create.

Just like a normal HTML table, a MySQL table is a big square divided into rows and columns. Each square is called a cell or field.


Each table in a database needs a title or name.
Each column in a table needs a title or name.


// set your infomation.

// connect to the mysql database server.
$link_id = mysql_connect ($dbhost, $dbusername, $dbuserpass);
echo "success in database connection.";

// select the specific database name we want to access.
if (!mysql_select_db($dbname)) die(mysql_error());
echo "success in database selection.";

// add a table to the selected database
$result="CREATE TABLE address_book (first_name VARCHAR(25), last_name VARCHAR(25), phone_number VARCHAR(15))";
if (mysql_query($result)){
echo "success in table creation.";
} else {
echo "no table created.";


There are 4 sections to the above example coding. The first two sections initialize the main variable information and use it to connect to the database server.

The third section tells the database server which database we want to work with. In this case, it is the "david_test" database we created on the previous tutorial page. The IF statement will stop the script and print out the SQL error if the mysql_select_db command produces a false result. If there is a positive result, the script will continue on.

The last section will create the TABLE giving it a name. It also creates the columns giving them a name and declaring what type of data will be entered into them. Taking a closer look...

$result="CREATE TABLE address_book (first_name VARCHAR(25), last_name VARCHAR(25), phone_number VARCHAR(15))";

A variable $result is being used to store the information for the up coming query command. This information can be entered directly into the query command itself, but storing it into a variable makes it easier to control and edit.

CREATE TABLE address_book( ) will create a table named "address_book" and create the following columns in it. The comma seperated list in the brackets will become the columns in the table. first_name VARCHAR(25) will name the first column as "first_name" and states that the data entered into this column will probably be a VARiable CHARacter length being 25 characters maximum. And so on for the second and third columns.

The IF statement will execute the actual mysql_query using the information stored in the variable. It will print out a success or failure depending on the results.

If that is all successful, this is what you will have so far visually...



Advertise in the banner along the top, Click here!

Valid XHTML 1.0 Transitional Valid CSS! Text Link Ads