Solved

MySQL query error

Posted on 2004-08-03
3
202 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
  • 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

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…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…

713 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