{Web Space Tutorials}

good tutorials, made easy

PHP - Setting Up A Database

Introduction

One of the reasons PHP is so great, is because of it's ability to communicate with a server and database. Here we will guide you through the basics of setting up your first database.

Prerequisites

For this tutorial it is recommended that you have:


How To Use php

For those of you who are completely new to php let's go over a few of the basics. PHP is a server side language, meaning that for any php code to work, it must be processed through a server. What that means for us is that to see your php code in action, you need to upload your web page to a server.

Additionally the extension of your page must be .php instead of .html. Don't worry all your normal html code will still work, just think of .php as an upgraded version of .html.


The second thing about php is that any php code must be inside of <?php opening tag and ?> closing tag.Now that we covered a bit of the basics, let's get started.


Hosting

There are many good hosting services out there, most of which allow you to set up your own database. This tutorial is going to use GoDaddy as the sample. In my experience GoDaddy has been an easy service to set up and host a database driven site with few hassles. Of course you are free to use any hosting service you like, as they all are likely similar to set up.


Creating Your Database

First you must buy a hosting account with your desired service. Don't worry hosting is generally cheap, being about 5$ a month on average. Buying your hosting account should be straight forward.

After your account is all set up it's time to get your database set up. With GoDaddy click the MY Account lick in the navigation then down the page under My Products you will see web hosting. Click there then Launch to open your control center.

In your control center highlight Databases at the top then click MySQL. Here is where you will create your database. Simply click Create Database then fill out the form and we want to enable direct database access so change that to a yes. After creation your database will be pending, simply wait until the database finishes pending and changes to Setup. Now we can edit our database.


Setting Up Your Database

With our database created, it's time to set up our database. Simply click the name of your database then input your username and password to log into phpMyAdmin. phpMyAdmin is the main way to edit databases directly. Once logged in on the left side you will see your database along with something named information_schema. Ignore that and click your database. Here you will see a bunch of new options.

  1. Structure - This link shows all the tables in your database.
  2. SQL - Here is where you can input code yourself to do things like create tables, select tables, input information, and much more.
  3. Search - Here is a quick way to search for information in yor database.
  4. Query - Here is a more detailed search. With Query you can specificly look for information in your tables with many different variables.
  5. Export - This is for exporting your database into a file, usefull to move your database if you ever need to.
  6. Import - Import a database file.

What we want is to click structure then at the bottom you should see create new table. For name use photos and give it 2 fields.


Creating A Table

The next screen you should see is where we make our table. There are a bunch of options here so let's go over them.

  1. Field - Name of the field.
  2. Type - This is the type of field it will be. For most cases you can simply leave it as VARCHAR which just means normal text.
  3. Length/values - Here you can set the maximum length that can be put in this field.
  4. Collation - This setting is rarely used. It changed the language setting the field is set for. We can leave this blank 99.9% of the time.
  5. Attributes - This setting is rarely changed as well.
  6. Default - This is what you want the field to have in it by default.
  7. Extra - This is any type of extra setting such as auto_increcement which is good for id numbers.

A table can have a infinite number of fields, it all depends on what you need the table to hold. Let's say this table is going to store photo information. We would need a field for the photo name as well as one for the photo's filename.

Once your all done, click save in the bottom right and there you go, your first completely set up database! Read our next tutorial for more information on php and what you can do with your database.



More Reading

This is a very brief tutorial of the most commonly used php headers. If you would like to read more on what php headers can do, you can read more about them at w3schools.


w3schools php