CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner: Need help writing a script that interacts with a MySQL DB: Edit Log



MDTech.us_MAN
Novice

Sep 29, 2012, 1:37 PM


Views: 12239
Need help writing a script that interacts with a MySQL DB

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"
}
}
}
}



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";
}


Maxwell D.


(This post was edited by MDTech.us_MAN on Sep 29, 2012, 1:54 PM)


Edit Log:
Post edited by MDTech.us_MAN (Novice) on Sep 29, 2012, 1:40 PM
Post edited by MDTech.us_MAN (Novice) on Sep 29, 2012, 1:41 PM
Post edited by MDTech.us_MAN (Novice) on Sep 29, 2012, 1:54 PM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives