CSCI 343 — Trying a little PHP

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" 
<html xmlns="">
table, td, th {
    border: 2px solid Green ;
Here are some friends.
  // Create connection

// Check connection
if (mysqli_connect_errno())
    echo "<p><em>Failed to connect to MySQL: " . mysqli_connect_error() .
$selectStatement =
  "SELECT AS friend1, AS friend2 " .
  "FROM Highschooler A, Highschooler B, Friend F " .
  "WHERE A.ID = F.ID1 AND B.ID = F.ID2 AND < " .
  "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>';


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.