-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhello_world.pl
44 lines (34 loc) · 1.05 KB
/
hello_world.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/perl
use strict;
use warnings;
use DBI;
print ("Hello world!\n");
my $dbh = DBI->connect("DBI:mysql:test",'root','root');
die "failed to connect to MySQL database:DBI->errstr()" unless($dbh);
# prepare SQL statement
my $sql = <<EOF;
SELECT lastname, firstname, extension
FROM employees
WHERE lastname = ? OR firstname = ?
EOF
my $sth = $dbh->prepare($sql)
or die "prepare statement failed: $dbh->errstr()";
my($lname,$fname,$ext);
my($name, $answer);
print("\nPlease enter the employee firstname or lastname:");
while(<STDIN>){
$name = $_;
chomp($name);
$sth->execute($name,$name) or die "execution failed: $dbh->errstr()";
# loop through each row of the result set, and print it
while(($lname,$fname,$ext) = $sth->fetchrow()){
print("$lname, $fname\t$ext\n");
}
print("\nDo you want to continue? (Y/N)");
$answer = <STDIN>;
chomp($answer);
last if $answer eq 'N';
print("\nPlease enter the employee firstname or lastname:");
}
$sth->finish();
$dbh->disconnect();