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:
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:
#!/usr/bin/perl use strict; use DBI; #my $username = 'root'; #my $dsn = 'DBI:mysql:mysql'; my $conn = DBI->connect('DBI:mysql:<DBName>;host=<ServerIP>', '<Userna +me>', '<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.

In reply to Need help writing a script that interacts with a MySQL DB! by MDTech.us_MAN

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.