PHP is open source scripting language. It\'s widely used to develop web applications.  Home Web Programming PHP Retrieving data from a database

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>


Author's URL: SweDesignz.com
PHP is open source scripting language. It\'s widely used to develop web applications. More PHP Tutorials: Featured Materials | Fresh Materials | More PHP Tutorials at LearnPHP.org

Reader's comments
comments vikas July 13, 2011 says:
how to phase the value into the databse in which yopu are used in the database
Reply
comments vikas July 13, 2011 says:
i will use the coding to fetch the data into the database
it is easy to fetch the record into the database

Reply
comments ajayan July 05, 2011 says:
how can store a image from a file in a variable name to send database
Reply
comments ajayan July 05, 2011 says:
i want to send an image from a file to database and retrieve it from data base
how it can

Reply
comments test March 29, 2011 says:
how to fetch value from database in PHP
Reply
comments arun October 19, 2010 says:
how get the value from the databasebetween two tables value?
Reply
Add comments to "Retrieving data from a database"

Captcha