# set the user and password
my $user = 'user';
my $pass = 'pass';
# now connect and get a database handle
my $dbh = DBI->connect($dsn, $user, $pass)
or die "Can’t connect to the DB: $DBI::errstr\n"
my $sth = $dbh->prepare('insert into subscribers(username, emailaddr)
values "jim", "jim@microsoft.com")');
Then you can execute it:
$sth->execute();
my $sth = $dbh->prepare("select username, email from subscribers");
$sth->execute;
while(@row = $sth->fetchrow_array()) {
print "$row[0]: $row[1]<br>";
}
As you can see, $sth->fetchrow_array returns an array of the results. If you like, you could write the loop like this to make it a bit more readable:
while(my($username, $email) = $sth->fetchrow_array()) {
print "$username: $email<br>";
}
[ add comment ] | permalink |




( 3 / 25 )
Calendar



