Solved

MySQL query error

Posted on 2004-08-03
3
201 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Suggested Solutions

Title # Comments Views Activity
PERL export multiple query results to a JSON file 1 173
compress files in RAR using perl 13 83
Check file date before getting folder path 1 80
Edit Odbc.ini using perl module 2 90
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 …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…

809 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