Home Tutorial Php Phpdatabase Setting up the database in PHP

 
 

Share on Google+Share on Google+
Setting up the database in PHP
Posted on: January 23, 2010 at 12:00 AM
Advertisement
In this chapter, we will tell you how to create a database and tables in MySQL and also how to implement into PHP. Before writing the database scripts in PHP, we should have a database to insert the information into and read it from.

PHP DATABASE Setup

Part-5(a) : Setting up the database

In this chapter, we will tell you how to create a database and tables in MySQL and also how to implement into PHP. Before writing the database scripts in PHP, we should have a database to insert the information into and read it from.

I think your mind raises some questions about how to use database information into PHP. Let's take an example : you can tell PHP to go and look into the database for a list of information or you can say for contact information that you would like to see on your website. Your whole contact information stored in the database and PHP scripts bring it from there. 

I will show you later in this part how to connect PHP scripts with database. Now, we should concentrate first  what is the procedure to construct database.

Database Construction

The best thing about MySQL database is that it has its own standard set up to create database which is contained tables. We can create different - different tables in one database and each table will be separate and contain distinct records in different fields.

The procedure of creation a MySQL database depends on host to host, though you will end up with a database name, a user name and a password. This information is necessary to log in to the database.

Indeed, you can directly login with your username or password, if you already installed PHPMyAdmin on you computer. If you didn't installed than you need to use PHP scripts to create database.

Here, we can create database in two ways :

1.With the help of PHPMyAdmin

2.With the help of PHP Script.

1. With the help of PHPMyAdmin : In PHPMyAdmin we can create the database or table so easily.

Let's see the example below :

Open your PHPMyAdmin by entering the certain URL on your browser that is : http://localhost/phpmyadmin and it will open the database window. It will look like :

Now, it is the time to create the database. I will call my database contact_roseindia. In the Create new database field type the name of your database called contact_roseindia and then click the create button.

The database has been created successfully and you can see that on the left side of the window it shows "No tables found in the database". We need to add a table so that we can insert some data into it.

In the above image you can see that there was one two field. The first one is  Create new table on database contact_roseindia field type enter the name of your table, I call my table name tbl_contact and the second one is Number of fields and I add 8 fields for our contact page and then press the go button.

Now, this is the time to add information for our table fields. Enter the following information in your table.

Name
Type
Default
Collation
Attributes
Length
Null
Index
A_I
Comments
id INT None None None 6 BLANK PRIMARY YES BLANK
first VARCHAR None None None 15 YES BLANK BLANK BLANK
last VARCHAR None None None 15 YES BLANK BLANK BLANK
phone VARCHAR None None None 20 YES BLANK BLANK BLANK
mobile VARCHAR None None None 20 YES BLANK BLANK BLANK
fax VARCHAR None None None 20 YES BLANK BLANK BLANK
email VARCHAR None None None 30 YES BLANK BLANK BLANK
web VARCHAR None None None 30 YES BLANK BLANK BLANK

When you are finished press the save button. You should see the following information:

By clicking contact_roseindia table on the left menu, we can add information in the database

Now, click the insert button on the top of the screen.

You can now enter your test data. Remember to leave the ID field blank because it will be auto generated.

If you noticed, I entered the two records in the table and then press the go button. MySQL will tell us the records are inserted in the table.

If you want to see the records in your table then click the browse button at the top. The records will be shown like below :

So, we have done it our database creation part with the help of PHPMyAdmin. In the next page I will show you how to display the data in the contact form.

Advertisement

Related Tags for Setting up the database in PHP:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: January 23, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com