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

Learn how you can maximize big data in the cloud with Apache Hadoop. Download this eBook now. Brought to you in partnership with Hortonworks.

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 DataFlow is an integrated platform that makes data ingestion fast, easy, and secure. Download the white paper now.  Brought to you in partnership with Hortonworks

Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

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

{{ parent.tldr }}

{{ parent.urlSource.name }}