Displaying our database results
In the first part you built the database and the table to store your data and now we are going to show you how to display this information .
The following script does the nitty gritty here , again change the ("hostname" , "username" , "password") to your own settings .
<?php
//make a connection with your details
$connection = mysql_connect("hostname" , "username" , "password")
or die ("Cannot make the connection");
//connect to the sampldb database with our connection details
$db = mysql_selectdb("sampledb" , $connection)
or die ("Cannot connect to the database");
//select all from sample table and store in $sql_query
$sql_query = "SELECT * FROM sample";
//store this in $result
$result = mysql_query($sql_query);
//if an entry exists
if(mysql_num_rows($result))
{
//loop through the fields
while($row = mysql_fetch_row($result))
{
//display the links $row[1] is the link , $row[0]
//is the id value
echo ("<a href=\"$row[1]\">$row[1]</a><br>");
}
}
//if no entry exists print a message
else
{
echo "no values in the database";
}
?>
That is all there is to it , to see all of this in action we have an example page , testing our db
Page 1 : adding to our database
Page 3 : delete from our database
|