Testing out my development environment with XAMPP, I created a HolaMundo.php file to try out my new MySQL database and some echo commands from PHP it seemed simple enough although the dollar signs are a little awkward to type. The file starts with a stage that just tells the HTML document that I will be using PHP programming language this can be inserted into any part of the HTML document. Next, I use mysql_connect this just passes my credentials to the database which is at localhost and username root with no password. after that, i have to call the Addressbook database somehow so I use mysql_select_db and that gets the database. Lastly, I create a variable to store the database in result this could have been called anything and store the query from colleague table. after this its just standard XHTML markup to prepare the document for the new data coming in. Next, I create a while loop while ($row = mysql_fetch_array($result)) results get passed into the while loop and iterates through to display id firstName lastName telephone email and favorite color and this is done with a simple echo command. and the last parts closes the current connection and cleans up memory.
<?php
mysql_connect("localhost", "root", "");
mysql_select_db("addressBook");
$result = mysql_query("SELECT * FROM colleague");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3c.org/TR/xhtml1/DTD/xhtml-transitional.dtd">
<html xmins="http://www.w3.org/1999/xhtml">
<head>
<title>Address Book</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
</head>
<body>
<h1> Address Book</h1>
<table border="1" cellpadding="2" cellspacing="3"
summary="Table holds colleague contact information">
<tr>e
<th>ID:</th>
<th>First Name:</th>
<th>Last Name:</th>
<th>Phone Number:</th>
<th>Email Address:</th>
<th>Favorite Color:</th>
</tr>
<?php
// loop threw all the table rows in database
while ($row = mysql_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['firstName'] . "</td>";
echo "<td>" . $row['lastName'] . "</td>";
echo "<td>" . $row['telephone'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['favoriteColor'] . "</td>";
echo "</tr>";
} // end of the while loop
//free memory an close database connection
mysql_free_result($result);
mysql_close();
?>
</table>
</body>
</html>
I always had a passion for the field of STEM (Science, Technology, Engineering, and Math) and I knew I wanted to do something to make a difference in the world. I just didn’t know where to start. I was an immigrant in a new country, grew up in a tough environment, and wasn’t sure how… Read More