Mysql and PHP Demo

A 3-tiered system

Accessing a user logon table

A common task is to set up a page which presents a logon screen, and to react to the submit button by checking a database to see if there is an account matching that username with that password. If the check is affirmative, then they are passed on to the logged-on system displays. If the check is negative, then they are returned to the logon screen, possibly with a prompt telling them the error (no such username or password incorrect. Commonly, the logon screen will include a link to a page which lets them establish a new account.

Querying a database table on another machine (streamer.rit.edu)

connected with streamer.rit.edu

username password
jeffs flintstone
someOtherUserName someOtherLongPassword

Questions to ask yourself:

  1. Why is a 3-tiered (or n-tiered) system better for a database-driven website than a 2-tiered system? (hint: think security)

The Source

And here is the source for the above (courtesy my Characters to Entities service):

<h3>Querying a database table on another machine (streamer.rit.edu)</h3>
<?php
  $server = "streamer.rit.edu";
  $user = "test";
  $password = "fr1end";
  $database = "test";
  $db = mysqli_connect( $server, $user, $password, $database );
  if( ! $db ) {
    die( "<p>Cannot connect: " . mysqli_connect_error() . "</p>\n" );
  }
  else {
    echo "<p>connected with " . $server . "</p>\n";
  }
?>
<table>
  <tr style="color: #cc0033; background-color: #cccccc;">
    <td>username</td>
    <td>password</td>
  </tr>
  <?php
    $q = mysqli_query( $db, "SELECT user, password FROM login" );
    while( $row = mysqli_fetch_row( $q ) ) {
      echo "    <tr><td>" . $row[0] . "</td>\n";
      echo "    <td>" . $row[1] . "</td></tr>\n";
    }
  ?>
</table>