2009-02-23 67 views
0

實際上有以下副本:How can I display data in table with Perl如何處理來自數據庫查詢的不同數量的項目?

這裏接受的答案適用於此處。所以做一些替代方案。


我想從Perl程序運行原始數據庫查詢並顯示結果給用戶。類似於select * from table。我想在HTML表格中顯示信息。 HTML表格中的列與返回的列相對應。

我有一些問題。我可以運行describe table查詢以返回表中的列數。但是,我將如何將返回結果中的信息存儲到數組中?

所以,如果我儲存的結果是這樣的:

while (($f1, $t2, $n3, $k4, $d5, $e6) = $sth1->fetchrow_array) 

在這種情況下,我只知道有,說四列(這是我從拿到描述表)。但是這個數字是動態的,可以根據表名改變。我不能根據這個數字來聲明我的變量。有什麼建議麼?

回答

4

嘗試:

print "<table>\n"; 

# display HTML header 
@cols = @{$sth->{NAMES_uc}}; 
print "<tr>".join("", map { "<th>${_}</th>" } @cols)."</tr>\n"; 

# display one HTML table row for each DB row 
while (my @row = $sth->fetchrow_array) { 
    print "<tr>".join("", map { "<td>${_}</td>" } @row)."</tr>\n"; 
} 
print "</table>\n"; 
2
while (my @row = $sth->fetchrow_array) 
{ 
     print "<tr>".join("", map{ "<td>${_}</td>" } @row)."</tr>" ; 
} 
+0

無需加入。打印需要一個列表。所以你可以說:print'',map(「​​$ _」),@row),'; – daotoad 2009-02-23 21:09:37

2

使用的技術中的答案(多個)其他問題建議 - 使用fetchrow_array抓取到一個數組:

while (my @row = $sth->fetchrow_array()) 
{ 
    ...process array... 
} 

或者使用到fetchrow_array()替代,如fetchrow_hashref()