Goal and information
In this lab, you create a small PHP presence on the Internet.
There are many web references on PHP. I found the “cheat sheets” on the SysAdminBlog to be pretty useful.
There is also lots of information at the official PHP pages including the following:
You can also take a look at bunch of information about our PHP configuration.
Being accessiable
You will need to make your web page accessible for this lab.
[…]$ chmod 755 public_html
You can always undo this at the end of the lab.
[…]$ chmod 700 public_html
Creating one page
Connect to the tutorial on Your first PHP-enabled page and create the “hello world” program. However, you can’t all store it in the web server root directory. You will need to store it in your public_html directory.
By the way, you can edit these pages in a simple text editor, such as gedit, or you can try the PHP development features of NetBeans.
Making a form
Now go to the page called Dealing with Forms and create the two PHP files described on that page.
After you get that working, take a look at the
language
reference description for the if
and figure out how to make your report generate the advice
“Don’t trust anyone over 30.” is the person
completing the form claims to be less than 21.
You can also try doing something interesting with a
for loop, if you found that one too easy. Notice that PHP
uses curly braces, just like C.
Connecting to a database
PHP has three different APIs for using MySQL. The Original MySQL API, which is pre-OOP, is officially deprecated. The two newer interfaces, MySQL Improved Extension or the PHP Data Objects interface to MySQL are recommended for new projects.
Nonetheless, here is an example of using the depracated API that is similar to our use of JDBC in the middleware lab.
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style>
table, td, th {
border: 2px solid Green ;
}
</style>
<title>Friends</title>
</head>
<body>
<h1>Friends</h1>
<p>
Here are some friends.
</p>
<?php
// Create connection
$con=mysqli_connect("joe.cs.unca.edu","csci343","","socialDB");
// Check connection
if (mysqli_connect_errno())
{
echo "<p><em>Failed to connect to MySQL: " . mysqli_connect_error() .
"</em></p>";
}
?>
<table>
<tr><th>Friend1</th><th>Friend2</th></tr>
<?php
$selectStatement =
"SELECT A.name AS friend1, B.name AS friend2 " .
"FROM Highschooler A, Highschooler B, Friend F " .
"WHERE A.ID = F.ID1 AND B.ID = F.ID2 AND A.name < B.name " .
"ORDER BY friend1, friend2 ;" ;
$result = mysqli_query($con, $selectStatement) ;
while($row = mysqli_fetch_array($result))
{
echo '<tr><td>' . $row['friend1'] . '</td><td>' . $row['friend2'] . '</tr>';
}
mysqli_close($con);
?>
</table>
</body>
</html>
Earlier you created a form that allowed the input of an age. Now create a form that allows the input of a friend. Then modify the large PHP program shown above to display all the friends of that friend.
Stop after 20 minutes of effort. You’ll be given a chance to polish up your program in a future homework assignment.