Starting the jokes project

Where are we?

You’ll need a way to apply your database skills. In this lesson, you’ll set up the Jokes application. As we work through this chapter, you’ll add pages to Jokes, until it’s complete.

This lesson’s goals

The Jokes application is much like DogToys and DogRock. It’s a template application, so it uses PHP for more than database work. You can try it.

In this lesson, you’ll:

  • Create a database on your computer.
  • Create a database user.
  • Test the connection.
  • Create a table.
  • Download the starting files to your machine.

Create the jokes database

Create a database called jokes on your machine. A brief recap of the steps:

  • Start Apache and MySQL.
  • Open phpMyAdmin.
  • Create the database.

Create a database user

Create a MySQL user for the database. You’ll need this to connect you PHP code to the database.

Test

Run the test program we used earlier. Here is the code again:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title></title>
  </head>
  <body>
    <h1>PHP-MySQL connection test</h1>
    <?php
    //DB connection data.
    //Probably leave $host alone.
    $host = 'localhost';
    //Set $db to the name of your database.
    $db = 'jokes';
    //Set user_name to the name of the MySQL user you made.
    $user_name = '[User name here]';
    //Set $password to the user's password.
    $password = '[Secret password here]';
    ?>
    <p>Trying to connect to MySQL.</p>
    <ul>
      <li>Host name: <?php print $host; ?></li>
      <li>Database name: <?php print $db; ?></li>
      <li>User name: <?php print $user_name; ?></li>
      <li>User password: (Secret)</li>
   </ul>
    <?php
    //Connect to the MySQL server.
    $db = new mysqli($host, $user_name, $password, $db);
    //Did it work?
    if ( mysqli_connect_error() ) {
      print '<p>Error! Could not connect to the database. ';
      print 'Error message: '.mysqli_connect_error().'</p>';
    }
    else {
      print '<p>W00f! It worked!</p>';
    }
    ?>
  </body>
</html>

Figure 1. Test a database connection

Change the code on lines 12 to 18 to match your database.

Create the jokes table

Create a jokes table in the jokes database. Here are the fields and types I used in mine:

  • joke_idINT, primary key, auto_increment
  • titleCHAR(30). A few words reminding me what the joke is about.
  • joke_textTEXT. The text of the joke itself.
  • funninessINT. How funny the joke is, from 1 (not funny) to 5 (hilarious).
  • when_addedDATE. When the joke was added to the database.

You can modify this list, if you want.

Add two or three jokes to the table.

Extract the starting files

Rather than asking you to write the entire application from scratch, I’ll give you all of the files with the database stuff removed. The PHP for templating is included.

Download the zip file. Extract it to a directory under your Web root, like c:\xampp\htdocs\jokes.

Try it in your browser.

Look through the files. Remind yourself how templating works.

Summary

You set up the Jokes application. It’s a starting point. You’ll add database code to it as we work though the chapter. You:

  • Created a database.
  • Created a database user.
  • Tested the connection.
  • Added a table and some data.
  • Extracted the starting files.

What now?

Let’s see how you let users add data to a database.


How to...

Lessons

User login

Log in problems? Try here


Dogs