Solved

MySQL query error

Posted on 2004-08-03
3
203 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 500 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…

734 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