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

Hortonworks Sandbox for HDP and HDF is your chance to get started on learning, developing, testing and trying out new features. Each download comes preconfigured with interactive tutorials, sample data and developments from the Apache community.

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:

Hortonworks Community Connection (HCC) is an online collaboration destination for developers, DevOps, customers and partners to get answers to questions, collaborate on technical articles and share code examples from GitHub.  Join the discussion.

Topics:

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}