Hi everyone,
I’m writing a Perl script that print all the record of the table if the script was run without any argument. If I run the script with a regular expression as an argument it should then print only the records from the table if the firstname, lastname, or the emailaddress of a record matches the regexp.
I have a table “customer_table” with the following fields:
Id int,
firstname varchar(64),
lastname varchar(64),
emailaddress varchar(64) not null primary key
city varchar (32),
Can some one help me and show me how to print only records that matches a given regexp, for example if I run (Usage: getRecord.pl
#> getRecord.pl M+
should return all record from the database if the first name, last name, or email address starts with a capital M
OR:
#> getRecord.pl
should return all records from the table, which I have it this way and it works just fine:
use DBI;
my $dataBaseDriver = “DBI:mysql”;
my $dataBaseName = “test”;
my $dataBaseUser = “user”;
my $dbUserPass = “user1234”;
my $tableName = “my_table”;
#—————————————#
# connect to the database #
#—————————————#
my $dbh = DBI->connect(“$dataBaseDriver:$dataBaseName”,
$dataBaseUser,
$dbUserPass,
{ RaiseError => 1,
AutoCommit => 0 }) || die “ERROR-1: Couldn’t
connect to $dataBaseHost: ” . DBI->errstr();
print “Connection to the database successful.\n”;
#—————————————#
# querying the database #
#—————————————#
my $sth = $dbh->prepare(“SELECT * FROM $tableName”);
$sth->execute();
#—————————————#
# print the data #
#—————————————#
while(my $ref = $sth->fetchrow_hashref()) {
print “ID: $ref->{‘Id’}\n”;
print “First Name: $ref->{‘firstname’}\n”;
print “Last Name: $ref->{‘lastname’}\n”;
print “Email Address: $ref->{’emailaddress’}\n”;
print “City: $ref->{‘city’}\n”;
}
$sth->finish();
$dbh -> disconnect();
Thanks for your help