The following code works for me but I want to know how to change the query so that it will select rows based on matches in two columns. In SQL this would be something like SELECT * FROM charge WHERE colA = 'cat' AND colB = 'dog'. I am not sure how to translate that into a perl script. I am looking to get input from terminal like, What is the value to match in columnA? cat <enter> What is the value to match in columnB? dog <enter>

#!/usr/bin/perl -w # first experiments in perl front end for mysql queries use strict; use warnings; use DBI; my $dbh = DBI->connect('dbi:mysql:mydatabase:localhost:3306',' +abc','xyz') or die "Couldn't connect to database: " . DBI->errstr; my $sth = $dbh->prepare('SELECT * FROM charge WHERE cpt = ?') or die "Couldn't prepare statement: " . $dbh->errstr; print "Enter cpt code> "; while (my $cptcode = <>) { # Read input from the + user my @data; chomp $cptcode; exit 0 if ($cptcode eq ""); $sth->execute($cptcode) # Execute the quer +y or die "Couldn't execute statement: " . $sth->errstr; # Read the matching records and print them out while (@data = $sth->fetchrow_array()) { my $cptanswer = $data[1]; my $insid = $data[2]; my $preauth = $data[3]; my $updated = $data[5]; print "\t$cptanswer\t$insid\t$preauth\t$updated\n"; } if ($sth->rows == 0) { print "No names matched `$cptcode'.\n\n"; } $sth->finish; print "\n"; print "Enter cpt code> "; } $dbh->disconnect;


In reply to select from mysql table based on two column match by abner

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.