データベース周りの操作をまとめたクラス quiz.php.inc
<?php
class Quiz {
  var $table='quiz';
  function Quiz() {}
  function connectDB($dbserver, $username, $password, $database) {
    @mysql_connect($dbserver, $username, $password);
    @mysql_select_db($database);
  }
  function query($query_string) {
    $result = @mysql_query($query_string);
    if (! $result) {
      printf("MySQL error(%d): %s\n", @mysql_errno(), @mysql_error());
      return NULL;
    }
    return @mysql_fetch_object($result);
  }
  function getAnswer($id) {
    return $this->query("SELECT `answer` FROM $this->table WHERE `id`=$id;");
  }
  function getQuestion($id) {
    return $this->query("SELECT `question`,`A`,`B`,`C`,`D` FROM $this->table WHERE `id`=$id;");
  }
  function addQuestion($question, $answer, $answers) {
    assert(in_array($answer, array('A', 'B', 'C', 'D')));
    assert(4 == count($answers));
    list($A, $B, $C, $D) = $answers;
    $this->query("INSERT INTO `$this->table` VALUES(NULL,`$question`,`$answer`,`$A`,`$B`,`$C`,`$D`);");
  }
}
?>
続きは、また今度。