LJ Archive
<HTML>
<Head>
<Title>Testing cookie retrieval</Title>
</Head>
<Body>
<H1>Testing cookie retrieval</H1>
{
  # Reset value of $outputstring
  my $outputstring = "";

  # Retrieve the cookie's value
  my $person_id = $LJ::query->cookie("person_id");

 # Bring in the package needed for accessing
 # MySQL
  use Mysql;

  # Connect to the MySQL server
  my $dbh = Mysql->connect("localhost", "test");

  # Create the query
  my $command =
"select firstname,lastname,email,birthdate ";
  $command .=
"from birthdays where person_id = $person_id";

  # Send the query for processing
  my $sth = $dbh->query($command);

  # If nothing came back, or if $person_id was
  # blank, then issue an error message, and let
  # the user enter a birthday
  if (($person_id == 0) || ($sth->numrows == 0))
  {
    $outputstring .=
      "<P>Error retrieving information.</P>\n";
    $outputstring .= "<P>person_id (cookie) = \"
      $person_id\".</P>\n";

    my $numrows = $sth->numrows;
    $outputstring .=
      "<P>Rows returned from table = \"
      $numrows\"</P>\n";

    $outputstring .=
     "<P><a href=\"/birthday.html\">";
   $outputstring .=
          "Enter your birthday</a></P>\n";
  }

  # If something came back, then grab values and
  # assign them
  else
  {
   while (my @arr = $sth->fetchrow)
   {
     my ($firstname, $lastname, $email,
            $birthdate) = @arr;
      $outputstring .= "<P>firstname = \"
     $firstname\"</P>\n";
      $outputstring .= "<P>lastname = \"
     $lastname\"</P>\n";
      $outputstring .= "<P>email = \"
     $email\"</P>\n";
      $outputstring .= "<P>birthdate = \"
     $birthdate\"</P>\n";
    }
  }
  # Return whatever is in $outputstring
  $outputstring;
}
</Body>
</HTML>
LJ Archive