in reply to Re^3: How to read multiple tables into arrays
in thread How to read multiple tables into arrays

I'd love to hire someone if I was rich. :) Anyway, this is what I attempted to do:
#$colleges = sth->fetchrow_array();
#while (my($colleges)=$colsth->fetchrow_array()){
# print "$colleges \n";
# $myQuery = "select d.departmentName from Colleges c join Departments d on c.college_id=d.college_id";
# $deptsth = &exeQuery($mydbh, $myQuery);
# while (my($departments)=$colsth->fetchrow_array()){
# print "$departments \n";
# }
#}
  • Comment on Re^4: How to read multiple tables into arrays

Replies are listed 'Best First'.
Re^5: How to read multiple tables into arrays
by eric256 (Parson) on Nov 10, 2008 at 23:03 UTC

    First: Please use <code></code> tags around your code.

    Second...and? What happens when you do that? What doesn't happen? What do you want to happen?


    ___________
    Eric Hodges
      I can't do it because the first dbh is not closed. I come up with a not so bright way to do it using fetchrow_array(), so now I have two arrays. One will hold an array of colleges while I query each department in the college. It doesn't look gracious, so I will take suggestions for improvement. Thanks a bunch.

      my(@colleges)=();
      while (my @ary = $mysth->fetchrow_array()){
      push(@colleges, @ary); # @ary is a reference
      }
      $mysth->finish();

      $i = 0;
      $j = 0;
      foreach(@college){
      print "colleges: ", @{colleges->$i}, "\n"; $myQuery = "select d.departmentName from Colleges c join Departments d on c.college_id=d.college_id where c.id=$j";
      $j++;
      $deptsth = &exeQuery($mydbh, $myQuery);
      while (my @ary = $deptsth->fetchrow_array())
      { push(@departments, @ary); # @ary is a reference
      print "departments: ", @{departments->$j}, "\n";
      $i++; }
      }

        Please Please Please put code tags around that code. It will make it actually readable. Then include enough code that we can actually look for improvements. Never include variables in SQL unless you have to. Use bind variables (the DBI docs have tons on this, use ? in the query and then you supply the data while running the query. I also don't know what exeQuery is but you probably don't need the & in front of it, and i would venture to bet you don't need that function at all.


        ___________
        Eric Hodges