Retrieving Data From a Database

Retrieving Data From a DatabaseBefore getting started, I'll assume you already know how to connect & select the database of your choose.

Retrieving data from a database is a common task for web database applications, the most common use is to display the data in a web page.

To get the data from the database, we'll need to use the SELECT command.

OK, lets say we have a table named "pets", containing pets names, types...etc

We want to display all the dogs within our page. Pay attention to the code comments.

<?php

// we'll select all the information in the database for five dogs.
$query "SELECT * FROM pets
              WHERE type = 'dog'
              LIMIT 5";

// actually execute the query.
$result = mysql_query($query);

?>

Now that we got the information, we'll need to display it:

<?php

// for every dog we selected, run the following block.
while ($row = mysql_fetch_array($result))
{
    // display information for each dog.
    echo "Name: $row[name], Age: $row[age], Breed: $row[breed] ";
}

?>

We've retrieved the data and displayed it. Of course you should make nicer, but you get the idea.

You can see the full script below:

<html>
<head>
   <title>Dogs</title>
</head>
<body>

<?php

// connect to database.
include("connect.php");

// we'll select all the information in the database for five dogs.
$query = "SELECT * FROM pets
              WHERE type = 'dog'
              LIMIT 5";

// actually "do" the query.
$result = mysql_query($query);

// for every dog we selected, run the following block.
while ($row = mysql_fetch_array($result))
{
   // display information for each dog.
   echo "Name: $row[name], Age: $row[age], Breed: $row[breed] ";
}

?>

</body>
</html>

Related Articles

  • Reading a Database
    Displaying data from a database is one of the most common uses of ASP.
    Heres some sample code:...
  • Do Not Be Fooled When Buying A Keyword Tool
    Whenever you want to rank well for your website or any of your webpages, you need to know the right keywords to target. Whenever you want to succeed in your pay-per-click (PPC) campaign, you need to know what the right keywords to bid are.
  • Inserting Rows into a Database
    This tutorial will show you how to insert a row of data into a database using ASP and an MS Access database. You can do it on one ASP file, but well have two files: one processor page (proc.asp), and one form page (form.asp). Then, well make the MS Access database that corresponds to the ASP.
  • Alternating Row Colors
    Learn how to alternate row colors from database output using ASP.
  • PHP News CMS
    Check out this great tutorial to find out all the details of Content Management Systems (CMS). Everything you need to know is right here.
  • Database Connection Strings
    Although our ASP database tutorials all use MS Access connection strings, there are many sorts of database software out there for use with ASP. This tutorial will show you how to connect with them...
  • Hit Counter
    Hit counters are used to track the amount of visitors there have been to a cetain page. I have looked through the internet and I have come up with 2 different ways to produce a hit counter.
  • Good Web Design - What is it?
    There is unlimited possibilities when it comes to designing a website. Flash, Database, content management, Client Login, etc. A question to ask yourself when deciding how to do your website is:...
  • ASP.NET in Dreamweaver 8
    In this article you will be able to use the new features of dreamweaver 8 to connect to a SQL database and return the results to a datagrid.
  • 3 Ways To Backup Your Website
    Data loss is the worst nightmare of people who own precious computer files, whether he is a website administrator or just a simple e-mail user. Sometimes, just one wrong push of a button can make all your files instantly vanish without a trace.

Contact Web Design Outsource and get started today

Need Website Designing, Development, Redesigning, Maintenance and SEO services or help growing your company's web presence? Request a free Quote Now.