为什么我使用此 Perl 代码会收到错误 500?
这是我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以尝试使用 CGI::Carp 在浏览器中获取错误消息:
(调试后不要将其保留,除非您希望用户看到可能泄露机密信息的神秘错误消息。)
您确定 Mysql 模块是访问数据库的正确方法吗?该模块已过时;现在每个人都使用DBI(可能在上面还有另一层)。您可以尝试使用 Hello World 类型脚本,该脚本仅
use Mysql;
但不会尝试实际连接到数据库,以查看它是否已安装。You can try using CGI::Carp to get an error message in your browser:
(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.简单的语法检查就能揭示问题。
您的程序包含
use strict
(正如它应该的那样)。但您还没有声明任何变量。A simple syntax check reveals the problem.
Your program includes
use strict
(as it should). But you haven't declared any of your variables.我同意@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.