in reply to Re: SQL query using dynamic array
in thread SQL query using dynamic array

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re^3: SQL query using dynamic array
by Corion (Patriarch) on Nov 18, 2008 at 20:17 UTC

    Your code makes little sense to me. It also doesn't seem to be very self-contained, as it contains syntax errors.

    What is the following code supposed to do?

    foreach $i(0..$#{$rows}) { foreach $j (0..$#{$rows->[$i]} ) { $rows->[$i][$j]; } }

    Have you tried replacing the query results with hardcoded data? Does the program work then as you want it to? If so, then the problem is with retrieving the data. If not, then the problem is how you handle the retrieved data.

    The code structure here doesn't make much sense:

    while ($i<=2) { my $placeholder = ":p_$j"; print "$placeholder\n"; my $rd = "$rows->[$i][0]"; print "$rd\n"; ################################ # # create a new EXCEL instance # ################################ my $Excelfile = "Q4 $rd PERFORMANCE RPT.xls"; my $excel = Spreadsheet::WriteExcel::Big->new("$Excelfile"); my $worksheet = $excel->addworksheet("Q4_STORE"); ########Create Subroutine to Select Data and insert into Worksheet#### +#### sub get_store_data {

    What is the while loop supposed to do, and why do you declare a subroutine in the while loop?

    Why do you spend six lines on a comment to a single line of code that says exactly what the comment says? Repeatedly? Removing these comments will make the code more understandable, because there is nothing of value added by a comment of "Prepare Query" when the code says $dbh->prepare($stmt).

    ############################ # # Prepare Query # ############################ my $sth = $dbh->prepare($stmt);

      What is the following code supposed to do?

      foreach $i(0..$#{$rows}) { foreach $j (0..$#{$rows->[$i]} ) { $rows->[$i][$j]; } }

      Answer: Each time the code runs I select distinct(id) from table. When I fetch the id's and put them into an array, they are all placed into array index '0'. I want to pass individual id's to the sql statement so I add indexes by the above logic where I iterate through by incrementing $i++.

      Have you tried replacing the query results with hardcoded data? Does the program work then as you want it to?

      while ($i<=2) { my $placeholder = ":p_$j"; print "$placeholder\n"; my $rd = "$rows->[$i][0]"; print "$rd\n";

      Answer: Yes. I can set the rd to equal a specific value and it returns the value result. If the array = ('A01','B88','C88',....'nth'), I want to only select * from table where id = 'A01'; pass 'A01' to email, then 'B88'....then 'C88'....'nth'.

      The while loop iterates through the array and changes the index value. The value changes as $i increments. The problem is the sql code only returns data for the first array index. All of the print statements change. All of the emails and spreadsheet data is changed. but only the first array index is populated in the emailed spreadsheets.

        What is the following code supposed to do?
        foreach $i(0..$#{$rows}) { foreach $j (0..$#{$rows->[$i]} ) { $rows->[$i][$j]; } }
        Answer: Each time the code runs I select distinct(id) from table. When I fetch the id's and put them into an array, they are all placed into array index '0'. I want to pass individual id's to the sql statement so I add indexes by the above logic where I iterate through by incrementing $i++.

        I don't see how your answer relates to the code snippet I posted. Please show a data structure for $rows and what changes you expect this code to make to that data structure.

        I did not mean setting $rd to a single specific value, but setting $rows to a premade dataset instead of something returned from an SQL query.

        Why are you sure that the SQL code "only returns data for the first array index."? What steps did you take to find out whether you get all data or just one row of data? Have you used Data::Dumper?

        You show a long block of code, but I have the feeling that you don't see the structure of your code, as I can't make out in your description what parts of the code you are talking about.