Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Writing sql query results stright to the file

Status
Not open for further replies.

JackTheRussel

Programmer
Joined
Aug 22, 2006
Messages
110
Location
FI
Hi all.

Can you tell me some better way to do this-

I make two queries because I need data from two columns:
name and age.

Persons
ID NAME AGE
1 john 22
2 kim 26
3 paul 32

And I do like this

1.select name from persons
2.push data to @name table

3.select age from persons
4.push data to @age table

5.Then I join these informations under the for-sentences
Code:
$empty =" ";
for (my $i=0; $i<@age; $i++)
{
     @row[$i] =$name[$i].$empty.$age[$i];
     
}
And I finally get right rows where I have name and age:

john 22
mike 26
paul 32

And then I write data to the file.
Code:
$size = @rows;
$file ='/home/xxx/file.txt';
open (H, ">>$file");

for (my $j=0; $j<$size; jt++)
{
    print H "$row[$j]\n";
}
close H;


So is there any better solution where I dont have to make two diffrent sql-querys? And is there some ready module which writes sql-query results stright to the file etc.. ?

Thank for help.
 
Bug:

Code:
for (my $j=0; $j<$size; jt++)

Should be:
Code:
for (my $j=0; $j<$size; j++)
 
Code:
my $pic_sql="SELECT name, age from persons;
my $pic_sth=$dbh->prepare($pic_sql);
$pic_sth->execute();
my @pic_results;
while(@pic_results=$pic_sth->fetchrow_array()){

# write to the file here
print "$pic_results[0] - $pic_results[1]<br>";
}

Keith
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top