查询数据库后,我无法再将数据和文本打印到浏览器
我正在上网络脚本课程,老实说,不幸的是,它排在我的网络、设计和分析课程之后。因此,我发现我遇到的问题可能很平常,但无法轻松找到解决方案。
我正在编写一个应该与 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
阅读评论后,您的程序损坏的原因是 query() 不执行 SQL 查询。因此,您可能正在调用未定义的子例程,除非这是您在其他地方定义的包装器。
这是我最初发布的有用提示,仍然适用:
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: