Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

MySQL query error

Posted on 2004-08-03
3
Medium Priority
?
206 Views
Last Modified: 2010-03-05
I have this query set up to print out to a file. In the foreach section, I am under the impression that it will go through each row of the database and print out the data that I have in the file print. It just prints the same record over 6 times, which is the number of COLUMS I have in the database:

        my $q2 = qq{ SELECT * FROM dejagnu };
        my $rs2 = $dbh->prepare ( $q2 );
        $rs2->execute();
        my @dejadata2 = @{$rs2->{NAME}};
        while (my $ref5 = $rs2->fetchrow_hashref() )
        {
            #write all the parameters to the .bashrc file
            open(fileOUT, ">.dejagnurc") || die "Could not open configuration file";
            flock(fileOUT, 2);
            print fileOUT "global env\n\n";
            print fileOUT "\# Flush out \"/data_logging\" or \"/rms\" for the PRODUCT env variable.\n";
            #dynamically changed variables
            foreach (@dejadata2) {
                print fileOUT "$ref5->{deviceType} {\n";
                print fileOUT " set dut(ip)       \"$ref5->{ip}\"\n";
                print fileOUT " set dut(subnet)   \"$ref5->{subnet}\"\n";
                print fileOUT " set dut(gateway)  \"$ref5->{gateway}\"\n";
                print fileOUT " set upsType       \"$ref5->{upsType}\"\n";
                print fileOUT "}\n";
            }
            close (fileOUT);
0
Comment
Question by:k41d3n
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 18

Accepted Solution

by:
kandura earned 2000 total points
ID: 11709321
Hi k41d3n,

1. I think you should open the file before you start to retrieve records.
2. You're looping over the column names with 'foreach (@dejadata2)', but you're printing the complete record in there. That means you'll get as many copies as there are columns. Since you're not actually using the column names, I think you can remove that foreach and just leave its loop body.

HTH,
Kandura
0
 
LVL 2

Author Comment

by:k41d3n
ID: 11709429
well dang, that did it. Thanks again Kandura.
0
 
LVL 18

Expert Comment

by:kandura
ID: 11709583
That was a quick one again :-)
I think you just made me pass ozo in the yearly Top 15, thanks! :-D
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question