perlquestion
MDTech.us_MAN
Hello All!
I am currently learning Perl CGI using a book I have. I now want to write a script that receives a person's name as an argument then, looks up the person in the database and returns the Name and Email.
I need help with this part:
<code>
my @results;
while (@results = $query1->fetchrow_array()) {
foreach(@results) {
if ($index = "1") {
print "\nName: $_\n";
$index = "2"
} else {
if ($index = "2") {
print "\nEmail: $_\n";
$index = "2"
}
}
}
}
</code>
Here is what I already have:
<code>
#!/usr/bin/perl
use strict;
use DBI;
#my $username = 'root';
#my $dsn = 'DBI:mysql:mysql';
my $conn = DBI->connect('DBI:mysql:<DBName>;host=<ServerIP>', '<Username>', '<Password>', { RaiseError => 1 }) || die "Could not connect to the database";
my $console = $ARGV[0];
my $query = "SELECT * FROM People where Name = '$console'";
my $query1 = $conn->prepare($query) || die "Error preparing query" . $conn->errstr;
$query1->execute || die "Error executing query" . $query1->errstr;
print "$query";
my $index = "1";
my @results;
while (@results = $query1->fetchrow_array()) {
foreach(@results) {
if ($index = "1") {
print "\nName: $_\n";
$index = "2"
} else {
if ($index = "2") {
print "\nEmail: $_\n";
$index = "2"
}
}
}
}
if ($query1->rows == 0 ) {
print "No Records", "\n";
}</code>
Maxwell D.