Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5352201
  • 博文数量: 1144
  • 博客积分: 11974
  • 博客等级: 上将
  • 技术积分: 12312
  • 用 户 组: 普通用户
  • 注册时间: 2005-04-13 20:06
文章存档

2017年(2)

2016年(14)

2015年(10)

2014年(28)

2013年(23)

2012年(29)

2011年(53)

2010年(86)

2009年(83)

2008年(43)

2007年(153)

2006年(575)

2005年(45)

分类: LINUX

2009-12-08 23:10:11

has asked for the wisdom of the Perl Monks concerning the following question:

Hey

Sorry, a newbie type of question, but somehow I never gotten this to work when I tried. Basically I want to retrieve all values from a MySQL database and print it out in a table. The below coding works and displays all the values I want it to display. However, It uses fetchrow_hash, which retrieves the first row values of the table and then calls a while statement using a fetchrow_array, which retrieves all the values except the first row. How can I make this code so it gives it a bit more efficiency by not having to call both fetchrow_hash and array? Isn't there a way just to do something like a while (%hash = $sth->fetchrow_array) to retrieve all the values at once? I tried this but the my script died on me and seemed to slow my server down dramatically? ahh~ I just have a feeling that this is such a simple newbie mistake that I'm doing wrong. It seems like the fetchrow-hash is retrieves ALL the mysql db results (including column names that are not listed in my SELECT statement?) and therefore theirs room for improving efficiency.

print < EOF #retrieve business plans $ci_blah = "mypassword"; use DBI; $dbh_m = DBI->connect('DBI:mysql:companies','myusername',$ci_bla +h) or die "Couldn't connect to database: " . DBI->errstr; $sql_m = "SELECT id,firstname,lastname,country,file_name,summ +ary,date FROM companies_db"; $sth_m = $dbh_m->prepare($sql_m) or die "preparing: ",$dbh_m- +>errstr; $sth_m->execute or die "executing: ", $dbh_m->errstr; $results = $sth_m->fetchrow_hashref; print " "; print " "; print " "; print " "; print " "; print " "; print " "; print " "; print " "; while (@data = $sth_m->fetchrow_array()) # keep fetching until # there's nothing left { print " "; print " "; print " "; print " "; print " "; print " "; print " "; print " "; print " "; } $sth_m->finish; $dbh_m->disconnect; print "
ID Name Company Country File Summary Date Submitted
$results->{'id'}$results->{'firstname'} $results{'lastname'}$results->{'company'}$results->{'country'}$results->{'file_name'}$results->{'summary'}$results->{'date'}
$data[0]$data[1] $data[2]$data[3]$data[4]$data[5]$data[6]$data[7]
";
 
 
 
my $sth = $dbh->prepare(q{
INSERT INTO sales (product_code, qty, price) VALUES (?, ?, ?)
}) or die $dbh->errstr;
while (<>) {
chomp;
my ($product_code, $qty, $price) = split /,/;
$sth->execute($product_code, $qty, $price) or die $dbh->errstr;
}
阅读(2021) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~