查询数据库后,我无法再将数据和文本打印到浏览器

发布于 2024-08-21 22:50:50 字数 1223 浏览 2 评论 0原文

我正在上网络脚本课程,老实说,不幸的是,它排在我的网络、设计和分析课程之后。因此,我发现我遇到的问题可能很平常,但无法轻松找到解决方案。

我正在编写一个应该与 MySQL DB 一起使用的 CGI 表单。我可以很好地在数据库中插入和删除。我的问题是在查询数据库时出现的。

我的代码编译得很好,当尝试通过浏览器“显示”数据库中的信息时,我没有收到错误,但数据和文本实际上并未显示。有问题的代码在这里:

print br, 'test';

my $dbh = DBI->connect("DBI:mysql:austinc4", "*******", "*******", {RaiseError => 1} );
my $usersstatement = "select * from users";
my $projstatment = "select * from projects";

# Get the handle
my $userinfo = $dbh->query($usersstatement);
my $projinfo = $dbh->query($projstatement);

# Fetch rows
while (@userrow = $userinfo->fetchrow()) {
    print $userrow[0], br;
}

print 'end';

此代码位于 if 语句中,由打印头、start_html、form、/form、end_html 包围。我只是想调试并找出发生了什么并打印语句 test 和 end。它打印出测试但不打印出结束。它也不会打印出我的数据库中的数据,这恰好是在我打印结束之前出现的。

我相信我正在做的是:

  • 连接到我的数据库
  • 形成一个字符串,其中包含对数据库的命令/请求
  • 获取我在数据库上执行的查询的句柄
  • 从我的句柄中获取一行
  • 打印我获取的行中的第一个字段从我的表格

但我不明白为什么我的数据不能像最终文本一样打印出来。我查看了数据库,它实际上包含数据库中的数据以及我试图从中获取数据的表。

这让我很困惑,所以我很感谢任何帮助。再次感谢。 =)

解决方案:

我使用的模块不支持我所包含的模块。这引出了另一个问题。我怎样才能检测到这样的错误?我的程序实际上可以正确编译,并且网页不会“中断”。除了仔细检查我使用的所有方法是否有效之外,我是否只是看到类似文本未显示的内容并假设发生了这样的错误?

I'm in a web scripting class, and honestly and unfortunately, it has come second to my networking and design and analysis classes. Because of this I find I encounter problems that may be mundane but can't find the solution to it easily.

I am writing a CGI form that is supposed to work with a MySQL DB. I can insert and delete into the DB just fine. My problem comes when querying the DB.

My code compiles fine and I don't get errors when trying to "display" the info in the DB through the browser but the data and text doesn't in fact display. The code in question is here:

print br, 'test';

my $dbh = DBI->connect("DBI:mysql:austinc4", "*******", "*******", {RaiseError => 1} );
my $usersstatement = "select * from users";
my $projstatment = "select * from projects";

# Get the handle
my $userinfo = $dbh->query($usersstatement);
my $projinfo = $dbh->query($projstatement);

# Fetch rows
while (@userrow = $userinfo->fetchrow()) {
    print $userrow[0], br;
}

print 'end';

This code is in an if statement that is surrounded by the print header, start_html, form, /form, end_html. I was just trying to debug and find out what was happening and printed the statements test and end. It prints out test but doesn't print out end. It also doesn't print out the data in my DB, which happens to come before I print out end.

What I believe I am doing is:

  • Connecting to my DB
  • Forming a string the contains the command/request to the DB
  • Getting a handle for my query I perform on the DB
  • Fetching a row from my handle
  • Printing the first field in the row I fetched from my table

But I don't see why my data wouldn't print out as well as the end text. I looked in DB and it does in fact contain data in the DB and the table that I am trying to get data from.

This one has got me stumped, so I appreciate any help. Thanks again. =)

Solution:

I was using a that wasn't supported by the modules I was including. This leads me to another question. How can I detect errors like this? My program does in fact compile correctly and the webpage doesn't "break". Aside from me double checking that all the methods I do use are valid, do I just see something like text not being displayed and assume that an error like this occurred?

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

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

发布评论

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

评论(1

你的笑 2024-08-28 22:50:50

阅读评论后,您的程序损坏的原因是 query() 不执行 SQL 查询。因此,您可能正在调用未定义的子例程,除非这是您在其他地方定义的包装器。

这是我最初发布的有用提示,仍然适用:

  1. 我希望您使用 CGI,使用 DBI 等...并使用 CGI::Carp 并使用 strict;
  2. 查看 /var/log/apache2/access.log 或 error.log 中的错误
  3. 认识到 CGI 脚本打印的第一件事必须是有效的标头,否则 Web 服务器和浏览器会变得不高兴,并且通常不会显示任何其他内容。
  4. 因为 #3 在执行任何操作之前首先打印标头,特别是在连接到数据库之前,脚本可能会终止或打印其他内容,否则错误或其他消息将在标头之前发出。
  5. 如果您仍然没有看到错误,请返回#2。
  6. 使用 CGI.pm 的 CGI 可以在终端会话中从命令行运行,而无需通过网络服务器。这也是一个调试的好方法。

Upon reading the comments, the reason your program is broken is because query() does not execute an SQL query. Therefore you are probably calling an undefined subroutine unless this is a wrapper you have defined elsewhere.

Here is my original posting of helpful hints, which still apply:

  1. I hope you have use CGI, use DBI, etc... and use CGI::Carp and use strict;
  2. Look in /var/log/apache2/access.log or error.log for the bugs
  3. Realize that the first thing a CGI script prints MUST be a valid header or the web server and browser become unhappy and often nothing else displays.
  4. Because of #3 print the header first BEFORE you do anything, especially before you connect to the database where the script may die or print something else because otherwise the errors or other messages will be emitted before the header.
  5. If you still don't see an error go back to #2.
  6. CGIs that use CGI.pm can be run from a command line in a terminal session without going through the webserver. This is also a good way to debug.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文