Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Austin Klenk
4,399 PointsPHP & MySql Address Book
I created a Address book using php & mysql. I Have 2 file one called list.php which lists out the contacts from the database, then another file called person.php. When the contacts get listed out each "contact" gets its own link with an id from the database.. The problem that i am having is that the list page does not show up when i am running from my php server, i can get the person.php working fine. all i get it bullets on the list.php page showing how many contacts are in the database but it doesn't show any contacts.
List.php
<html>
<head>
<title>Retrieve data from the database</title>
</head>
<body>
<ul>
<?php
// Connect to database server
mysql_connect("localhost", "root", "root") or die (mysql_error ());
// Select database
mysql_select_db("user_list") or die(mysql_error());
// SQL query
$strSQL = "SELECT * FROM people ORDER BY FirstName DESC";
// Execute the query (the recordset $rs contains the result)
$rs = mysql_query($strSQL);
// Loop the recordset $rs
while($row = mysql_fetch_array($rs)) {
// Name of the person
$strName = $row['FirstName'] . " " . $row['LastName'];
// Create a link to person.php with the id-value in the URL
$strLink = "<a href= 'person.php?id= " . $row['id'] . "'>" . $strNavn . "</a>";
// List link
echo "<li>" . $strLink . "</li>";
}
// Close the database connection
mysql_close();
?>
</ul>
</body>
</html>
person.php
<html>
<head>
<title>Retrieve data from database</title>
</head>
<body>
<dl>
<?php
// Connect to database server
mysql_connect("localhost", "root", "root") or die (mysql_error ());
// Select database
mysql_select_db("user_list") or die(mysql_error());
// Get data from the database depending on the value of the id in the URL
$strSQL = "SELECT * FROM `people` WHERE id = " . $_GET["id"];
$rs = mysql_query($strSQL);
// Loop the recordset $rs
while($row = mysql_fetch_array($rs)) {
// Write the data of the person
echo "<dt>Name:</dt><dd>" . $row["FirstName"] . " " . $row["LastName"] . "</dd>";
echo "<dt>Phone:</dt><dd>" . $row["Phone"] . "</dd>";
echo "<dt>Birthdate:</dt><dd>" . $row["BirthDate"] . "</dd>";
}
// Close the database connection
mysql_close();
?>
</dl>
<p><a href="list.php">Return to the list</a></p>
</body>
</html>
3 Answers
Juan Jose Cortes Guzman
7,894 PointsHi, i think the problem is in the variable $strNavm, it doesn't exists. The name of the person is in the variable $strName. Replace $strNavm with $strName and the code should work well.
Austin Klenk
4,399 PointsThanks Man!! that worked
Juan Jose Cortes Guzman
7,894 PointsYou're welcome