为什么我用这个perl代码得到错误500?

时间:2011-10-26 04:46:59

标签: mysql perl

这是我的/ perl-bin /目录中的perl代码,但出于某种原因,当我尝试从MySQL获取内容时,我一直收到错误500

这是代码

#! /usr/bin/perl

 use warnings;
 use strict;

 print "Content-type: text/html\n\n";
 print "<!DOCTYPE html>\n";
 print "<html>\n";
 print "<head>\n";

 print "<title>00000000000000000000</title>";
 print "<link rel='stylesheet' type='text/css' href='style.css' />";

 print "</head>\n";
 print "<body>\n";

 print "<div id='top'></div>";
 print "<div id='header'>";
 print "<div id='logo'></div>";

 print "</div>";

 use Mysql;

# MYSQL CONFIG VARIABLES
$host = "localhost";
$database = "000_ooo";
$tablename = "users";
$user = "000_root";
$pw = "l00V0r009;00XE_%0q;U00000000000000";

# PERL MYSQL CONNECT()
$connect = Mysql->connect($host, $database, $user, $pw);

# SELECT DB
$connect->selectdb($database);

# DEFINE A MySQL QUERY
$myquery = "SELECT * FROM $tablename";

# EXECUTE THE QUERY FUNCTION
$execute = $connect->query($myquery);

# HTML TABLE
print "<table border='1'><tr>
<th>id</th>
<th>product</th>
<th>quantity</th></tr>";

# FETCHROW ARRAY

while (@results = $execute->fetchrow()) {
    print "<tr><td>"
    .$results[0]."</td><td>"
    .$results[1]."</td><td>"
    .$results[2]."</td></tr>";
}

print "</table>";

 print "</body>\n";
 print "</html>\n";

3 个答案:

答案 0 :(得分:8)

您可以尝试使用CGI::Carp在浏览器中收到错误消息:

use CGI::Carp qw(fatalsToBrowser);

(除非您希望用户看到可能泄露机密信息的神秘错误消息,否则请勿在调试后将其保留。)

您确定Mysql模块是访问数据库的正确方法吗?那个模块已经过时了;这些天每个人都使用DBI(可能还有另一层)。您可以尝试只执行use Mysql;的Hello World类型脚本,但不会尝试实际连接到数据库,以查看它是否已安装。

答案 1 :(得分:5)

简单的语法检查揭示了问题。

$ perl -c mysql
Global symbol "$host" requires explicit package name at mysql line 26.
Global symbol "$database" requires explicit package name at mysql line 27.
Global symbol "$tablename" requires explicit package name at mysql line 28.
Global symbol "$user" requires explicit package name at mysql line 29.
Global symbol "$pw" requires explicit package name at mysql line 30.
Global symbol "$connect" requires explicit package name at mysql line 33.
Global symbol "$host" requires explicit package name at mysql line 33.
Global symbol "$database" requires explicit package name at mysql line 33.
Global symbol "$user" requires explicit package name at mysql line 33.
Global symbol "$pw" requires explicit package name at mysql line 33.
Global symbol "$connect" requires explicit package name at mysql line 36.
Global symbol "$database" requires explicit package name at mysql line 36.
Global symbol "$myquery" requires explicit package name at mysql line 39.
Global symbol "$tablename" requires explicit package name at mysql line 39.
Global symbol "$execute" requires explicit package name at mysql line 42.
Global symbol "$connect" requires explicit package name at mysql line 42.
Global symbol "$myquery" requires explicit package name at mysql line 42.
Global symbol "@results" requires explicit package name at mysql line 52.
Global symbol "$execute" requires explicit package name at mysql line 52.
Global symbol "@results" requires explicit package name at mysql line 54.
Global symbol "@results" requires explicit package name at mysql line 55.
Global symbol "@results" requires explicit package name at mysql line 56.
mysql had compilation errors.

您的计划包括use strict(应该如此)。但是你还没有声明任何变量。

答案 2 :(得分:1)

我同意同意@cjm。您绝对应该至少使用DBI模块。此外,为了美观,最好在所有其他代码之前将所有“使用”语句保留在代码的顶部。

我要确定的另一件事是您使用的用户名和密码确实有用。尝试通过命令行登录mysql,如果有,请连接到db并确保表存在。也可以将这些变量排除在等式之外。