为什么我使用此 Perl 代码会收到错误 500?

发布于 2024-12-11 16:37:52 字数 1506 浏览 0 评论 0原文

这是我的 Perl 代码,位于 /perl-bin/ 目录中,但由于某种原因,当我尝试从 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";

Here is my perl code located in /perl-bin/ directory but for some reason when i try to get something from MySQL i keep getting Error 500

here is the code

#! /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";

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

﹉夏雨初晴づ 2024-12-18 16:37:52

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

use CGI::Carp qw(fatalsToBrowser);

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

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

You can try using CGI::Carp to get an error message in your browser:

use CGI::Carp qw(fatalsToBrowser);

(Don't leave this in after debugging unless you want your users to see cryptic error messages that might reveal confidential information.)

Are you sure the Mysql module is the right way to access the database? That module is obsolete; everybody uses DBI these days (possibly with another layer on top). You might try a Hello World type script that just does use Mysql; but doesn't attempt to actually connect to the database, to see if it's even installed.

三生路 2024-12-18 16:37:52

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

$ 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 (正如它应该的那样)。但您还没有声明任何变量。

A simple syntax check reveals the problem.

$ 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.

Your program includes use strict (as it should). But you haven't declared any of your variables.

爱你不解释 2024-12-18 16:37:52

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

我要确保的另一件事是您使用的用户名和密码确实有效。尝试通过命令行登录 mysql,如果登录,请连接到数据库并确保该表存在。将这些变量也从方程中剔除。

I agree agree with @cjm. You should definitely be using the DBI module at a minimum. Also, for aesthetics, its best to keep all of your 'use' statements at the top of your code, prior to any other code.

One other thing I would make sure of is that the username and password you are using actually work. Try logging into mysql via the command line, if they do, connect to the db and also ensure the table exists. Take those variables out of the equation as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文