How to Set Up and Use a Database in a Website
By combining a website with a database, you can have website pages that are built "on the fly" by retrieving information from the database dynamically in response to requests from visitors. To set up and use a database on your website, you'll need to configure a database system on your Web host and modify your Web pages so they can access a software module that connects to the database.
Instructions
-
-
1
Configure your website host to enable a database system. Most website hosting services provide a control panel that can be used to configure the database system. For example, many Web hosting services provide the cPanel website control panel for website owners to use. cPanel has a database section with a button for activating MySQL, which is a popular open source database system.
-
2
Create a simple table in the database for testing. The SQL command "CREATE TABLE" can be used to create a database table. For example, you could create a table called "members" using the fields "firstname," "lastname" and "email." The SQL command for this example is: "CREATE TABLE member (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
firstname TEXT,
lastname TEXT,
email DATE NOT NULL
) DEFAULT CHARACTER SET utf8"
Add a few records to the new table using the SQL "INSERT" command. (see Resources)
-
-
3
Write a software module to connect with the database. With a text editor such as Notepad, write software code for a routine to connect with the database, for example by using the "mysqli_connect" PHP command. Once a a connection is established, the PHP software can call the "mysqli_select_db" PHP command and then use the "mysqli_query" command to retrieve results from the database. PHP is a commonly supported language and is often available on website hosts offering the MySQL database. Your website host will have a features list indicating which languages are supported.
-
4
Build a Web page to work with the database. Add code to your Web page (using a text editor) to invoke the software module. Web page database code will normally pass a value (or parameter) to the software module, which will access the database and use the parameter to retrieve one or more records. For example, the Web page could pass a first name to the software module as a parameter. The software module could then use the first name when accessing the database and retrieve any records having that first name. The software module could then supply the retrieved records to the Web page for display on a Web browser.
-
5
Test your website database. Open your Web page in a browser and evaluate the database functions to ensure that the software module correctly accessed and retrieved the data from your database. Make any necessary corrections to your software module or Web page code. A common problem can include not having the correct name, location or password for the database, table, or record fields programmed in your software module. Once your basic database setup is working, you can create database tables and software modules to expand your website.
-
1
Tips & Warnings
Databases can also be used to store information gathered from website visitors.