Solved

MySQL query error

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

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.…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.

758 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now