Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Retrieve Data from Database Table

DZone's Guide to

Retrieve Data from Database Table

· Big Data Zone ·
Free Resource

The open source HPCC Systems platform is a proven, easy to use solution for managing data at scale. Visit our Easy Guide to learn more about this completely free platform, test drive some code in the online Playground, and get started today.

Let's see how to retrieve information from the database table we created called "friends":

// Collects data from "friends" table
$data = mysql_query("SELECT * FROM friends")
or die(mysql_error());

And we will then temporally put this information into an array to use:

// puts the "friends" info into the $info array
$info = mysql_fetch_array( $data );

Now let's print out the data to see if it worked:

// Print out the contents of the entry
Print " Name: ".$info['name'] . " ";
Print " Pet: ".$info['pet'] . "
";

However this will only give us the first entry in our database and development Best Practices with Chapter one. In order to
retrieve all the information, we need to make this a loop. Here is an example:

while($info = mysql_fetch_array( $data ))
{
Print " Name: ".$info['name'] . " ";
Print " Pet: ".$info['pet'] . "
";
}

So let's put all the these ideas together to create a nicely formatted table with this final php code:

// Connects to your Database
mysql_connect("your.hostaddress.com", "username", "password") or
die(mysql_error());
mysql_select_db("Database_Name") or die(mysql_error());
$data = mysql_query("SELECT * FROM friends")
or die(mysql_error());
Print "";
while($info = mysql_fetch_array( $data ))
{
Print "";
Print " ";
Print "";
}
Print "
Name: ".$info['name'] . " Pet: ".$info['pet'] . "
";
?>
Next we will retrieve the information from the database table we created called "friends"

 // Collects data from "friends" table 
 $data = mysql_query("SELECT * FROM friends") 
 or die(mysql_error()); 

And we will then temporally put this information into an array to use:

Managing data at scale doesn’t have to be hard. Find out how the completely free, open source HPCC Systems platform makes it easier to update, easier to program, easier to integrate data, and easier to manage clusters. Download and get started today.

Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}