How to Link a Database to a Web Page

by Sue Smith
Data-driven websites allow companies to use a single data store across all systems.


Hemera Technologies/ Images

The content of any website for a business or organization comprises data, including text, images, media and numerical values. Using a database to store this data is an efficient approach for many sites. If your site's data is stored in a database -- for example, using a database management system such as MySQL -- you may face the task of presenting the data within your Web pages. This process involves connecting to the database, querying it for data and presenting the data in HTML, often by using a server side scripting language such as PHP.

1. Prepare your database user account details. Database systems use accounts, with specific levels of access to each user. Your account details should include a username and password. Locate these details, if necessary copying them into a file. You will also need the name and location of your database. Find all of these details before you start coding. Your Web host should be able to help you with this information if you cannot find it.

2. Connect to your database. You will need to use one or more server side scripts to connect to your database. The following example code demonstrates making a database connection to a MySQL system within a PHP script: <?php mysql_connect("database_host", "username", "password"); ?> You will need to alter the host address, username and password to reflect your own account. The process for making a connection is similar for other database systems and programming languages.

3. Query your data. In most cases scripts use SQL (Structured Query Language) to retrieve specific sets of data from databases. These SQL queries can execute from inside a server side script. The following sample query demonstrates retrieving all records in a table named "Customers": SELECT * FROM Customers The following code demonstrates executing this query in PHP: $customer_result = mysql_query("SELECT * FROM Customers"); The variable contains the result data following the query.

4. Output your data. Once you retrieve the data from your database, you can present it within your site pages, which are structured in HTML markup. The following code demonstrates writing the query results into a page within HTML structures: while($customer_row = mysql_fetch_array($customer_result)) { echo "<p>".$customer_row['CustName']."</p>"; } In this case the while loop iterates through each record in the "Customer" table, writing the value from a "CustName" field into the page as part of a paragraph element. You will need to alter the code to reflect the fields in your database table and the HTML structures you want to display them within.

5. Test your script. Once you have your database connection script complete, or partially complete, upload it to your server to test it. If you encounter errors, check your database account details as well as the structure of your tables. Once you have established that you can connect to the database successfully in your script, you can build on the basic code to present your data to site users.


  • If you are creating databases yourself, learning about relational database design will help you to build efficient data structures.


  • If your scripts contain multiple database connection and query strings, they can cause unnecessary network traffic.

About the Author


Photo Credits

  • Hemera Technologies/ Images
Suggest an Article Correction