|-USER------|
| id | (a unique identifier for the row)
| views | (count of how many pages user has viewed)
| last_ip | (the last IP address user used)
| last_time | (the last time we saw the user)
| url | (the user's URL)
|-----------|
####
|-ALLIP-----|
| id | (a unique identifier for the row)
| user_id | (the id of the user that used this IP)
| ip | (the IP address)
| time | (the time it was seen)
|-----------|
####
my $sql = <prepare( $sql );
if ( $sth && $sth->execute( $user_id ) )
{
while ( my $row = $sth->fetchrow_arrayref() )
{
print( "IP address " . $row->[0] . "\n" );
}
$sth->finish();
}
else
{
die( "Database error " . $dbh->errstr . "\n" );
}
####
my $found_id = undef;
my $sql = <prepare( $sql );
if ( $sth && $sth->execute( $url ) )
{
if ( my $row = $sth->fetchrow_arrayref() )
{
$found_id = $row->[0];
}
}
else
{
die( "Database error " . $dbh->errstr . "\n" );
}
if ( $found_id )
{
# perform update
$sql = <prepare( $sql );
if ( $sth && $sth->execute( $last_ip, time(), $found_id ) )
{
if ( my $row = $sth->fetchrow_arrayref() )
{
$found_id = $row->[0];
}
}
else
{
die( "Database error " . $dbh->errstr . "\n" );
}
}
else
{
# perform insert
# code left to read as an exercise
}