2013-05-21 65 views
1

我试图打印表格中的网页上的数据库表的所有值。我正在使用.cgi扩展名的perl文件。每当我尝试运行我得到的代码错误“全局符号需要明确包名”应显示的数据库表的.The行的onLoad但它不会发生..错误 - 全局符号需要明确的包名

我已经尝试了很多,但不明白什么是错的代码..

请帮助..

代码people.cgi文件..

#!/usr/bin/perl 
use CGI; 
use DBI; 
use strict; 
use warnings; 
print "Content-type:text/html\r\n\r\n"; 
#$q = CGI->new; 
#print $q->header; 
my $dsn = "DBI:mysql:Demo:localhost"; # Data source name 
my $username = "mint";     # User name 
my $password = "MINT123";    # Password 
my $dbh; 
my $sth;        # Database and statement handles 
$dbh = DBI->connect($dsn, $username, $password); 

$sth = $dbh->prepare("SELECT * from people"); 

$sth->execute(); 

print "<h1>ganesh</h1>"; 
print "<table > 
<tr> 
<th>ID</th> 
<th>Name of People Involved</th> 
<th>Position</th> 
<th>Roles(A user can have multiple roles)</th> 
<th>Notes</th> 
</tr>"; 
while($href = $sth->fetchrow_hashref) { 
    print "<tr>"; 
    print "<td>$$href{'id'}</td>"; 
    print "<td>$$href{'name'}</td>"; 
    print "<td>$$href{'pos'}</td>"; 
    print "<td>$$href{'role'} </td>"; 
    print "<td>$$href{'notes'}</td>"; 
    #print "<td><input type='text' value=\"$$href{'display_name'}\" id =\"dis-$$href{'windows_id'}\" readonly> </td>"; 
    #print "<td><input type='text' value=\"$$href{'email_id'}\" readonly> </td>"; 
    print "</tr>"; 
} 
print "</table>"; 

$sth->finish(); 
$dbh->disconnect(); 

错误消息..

Global symbol "$href" requires explicit package name at people.cgi line 31. 
Global symbol "$href" requires explicit package name at people.cgi line 34. 
Global symbol "$href" requires explicit package name at people.cgi line 35. 
Global symbol "$href" requires explicit package name at people.cgi line 36. 
Global symbol "$href" requires explicit package name at people.cgi line 37. 
Global symbol "$href" requires explicit package name at people.cgi line 38. 
Execution of people.cgi aborted due to compilation errors. 

我的MYSQL table..It的结构具有14个coloumns ......它里面没有数据...

enter image description here

+2

在'while'循环中声明'$ href'与'my':'while(my $ href = ...)'。 – amon

+0

我做到了这一点,并试图通过输入它的网址,其中包含它的位置在我的网页浏览器的服务器上运行该文件,但我收到消息“文件未找到”!! ......为什么? – Lucy

+0

[全局符号需要显式包名称]的可能重复(http://stackoverflow.com/questions/3141412/global-symbol-requires-explicit-package-name) –

回答

3

$href应该被定义(通常用于局部/词汇变量的my),在这种情况下,它将范围放在while循环中。

while (my $href = $sth->fetchrow_hashref) { .. } 
+0

是的,我做到了,但现在我得到一个错误说:“使用未初始化的值连接(。)或在people.cgi第37行的字符串。”一世。e在行“print”​​$$ href {'notes'}“; ...为什么是这个??? – Lucy

+0

'%$ href'的某些哈希值是'undef'和pragma'使用警告;'alert当打印'undef'值时,在打印前可以用'$ _ // =“”替换所有的undef值为空值%$ href;' –

+0

我编辑过我的帖子,显示表的结构。在上面的代码中有什么变化? – Lucy

2

每当你得到一个Perl的警告或错误,你不明白,你应该添加use diagnostics到你的代码和Perl会给你有关该问题的更多细节。在这种情况下,它会说:

你曾说过“用严格的”或“使用严格瓦尔”,这表明 所有变量必须被词法范围(使用“我”或“国家” ), 事先声明使用“我们”,或明确资格说这 包全局变量在(使用“::”)

更新:一些补充回答的意见进一步的问题下面。

如果该列可以包含NULL,并且打算打印这些列,那么除非您做了一些处理,否则您将得到“undefined value”警告。

最简单的修复方法是尽快清理哈希。我想用这样的事情在循环块的顶空字符串替换未定义的值:

$href->{$_} // '' for keys %$href; 

我也注意到,您使用的丑陋$$hash_ref{key}语法。不知道你从哪里挑选的,但通常认为$hash_ref->{key}看起来更清洁。

+0

是的,我做到了,并添加了关键字“我的”,并作出声明“while(my $ href = $ sth-> fetchrow_hashref){..}”现在我得到一个错误,说“使用未初始化的值连接(。)或字符串在people.cgi第37行。“即在行”打印“​​$$ href {'notes'}“”..为什么? – Lucy

+0

这不是一个错误,它是一个警告。你的表中的便条栏是否允许包含空值?数据库中的空值在Perl中变为undef。 –

+0

是的,它允许包含空值..我已经编辑了我的帖子,上面显示了表格的结构。在上面的代码中要做什么变化? – Lucy