- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
sub completecheckout {
$cryptedcard = md5_hex($cardnum . $salt);
$grabcart = qq~select pid from cart where uid='$cookievalue'~;
$dbh = DBI->connect($connectionInfo, $user, $passwd);
$sth = $dbh->prepare($grabcart);
$sth->execute();
while (@row = $sth->fetchrow_array()) {
$insert = qq~insert transaction (uid, pid, cctype, ccnum)
values ('$cookievalue', '$row[0]', '$cardtype',
'$cryptedcard')~;
$dbh = DBI->connect($connectionInfo, $user, $passwd);
$sth = $dbh->prepare($insert);
$sth->execute();
}
$select = qq~select * from registered where id in
(select uid from transaction
where uid='$cookievalue')~;
$dbh = DBI->connect($connectionInfo,$user,$passwd);
$sth = $dbh->prepare($select);
$sth->execute();
@userinfo = $sth->fetchrow_array();
print header;
print qq~<html><head><title>YAY</title></head><body><p>CHECK MYSQL<p><p>@row</p></body></html>~;
}
我正在尝试通过表格购物车进行解析,并在用户单击最终结帐按钮时将与用户关联的所有项目插入到交易表中。上面的代码只会将最后一行插入交易表。
这里是多次插入的代码,但不起作用,因为每隔一段时间 $product 都是空的。
sub completecheckout {
$cryptedcard = md5_hex($cardnum . $salt);
$grabcart = qq~select pid from cart where uid='$cookievalue'~;
$dbh = DBI->connect($connectionInfo,$user,$passwd);
$sth = $dbh->prepare($grabcart);
$sth->execute();
@cart = $sth->fetchrow_array();
foreach $product (@cart) {
$insert = qq~insert transaction (uid, pid, cctype, ccnum)
values ('$cookievalue', '$product', '$cardtype',
'$cryptedcard')~;
$dbh = DBI->connect($connectionInfo,$user,$passwd);
$sth = $dbh->prepare($insert);
$sth->execute();
}
$select = qq~select * from registered where id in
(select uid from transaction
where uid='$cookievalue')~;
$dbh = DBI->connect($connectionInfo,$user,$passwd);
$sth = $dbh->prepare($select);
$sth->execute();
@userinfo = $sth->fetchrow_array();
print header;
print qq~<html><head><title>YAY</title></head><body><p>CHECK MYSQL<p><p>@userinfo</p></body></html>~;
}
谁能解释为什么会这样?我一直在整个脚本中使用带有 fetchrow_array 的 while 循环来创建链接到数据库的表。
最佳答案
首先,您需要养成更好地格式化代码的习惯。如果格式模仿逻辑,它确实有助于遵循逻辑流。
其次,请打开use strict
并习惯于在尽可能接近其使用点的地方声明变量。
第三,不要使用全局变量。您的子例程使用 $cardnum
、$salt
、$cookievalue
和(大概)在子例程之外定义的其他几个变量。它们都应该作为参数传递到子程序中。
我从之前的谈话中知道你对学习 Perl 没有兴趣,你只是想通过你的大学坚持的类(class)。所以我应该明确一点,上面的所有建议都与 Perl 无关。对于任何编程语言,这都是很好的一般性建议。
现在,具体问题。
每当您想运行数据库查询时,您都在创建一个新的 $dbh
。为什么不只连接一次然后重用该变量。单个 $dbh
可以支持同时执行多个查询。
正如 Matt 在评论中指出的那样,您正在覆盖 $sth
。正如我上面所说,$dbh
可以支持多个并发查询,但每个查询都需要自己的语句句柄。所以你可以这样做:
my $dbh = DBI->connect(...);
my $select_sth = $dbh->prepare($select_sql);
$select_sth->execute;
while (my @row = $select_sth->fetchrow_array) {
my $insert_sth = $dbh->prepare($insert_sql);
$insert_sth->execute;
}
请注意我是如何 a) 重用相同的 $dbh
和 b) 在循环中声明 $insert_sth
以便它仅在尽可能短的时间内可用.
如果您对 Perl 感兴趣,我还会向您展示如何通过在 SQL 中使用绑定(bind)点并将额外参数传递给 execute()
来提高代码效率。我还建议将原始 HTML 从您的程序中移出并使用模板引擎。但我强烈怀疑您不会感兴趣。
关于mysql - while 循环只存储来自 @row=$sth->fetchrow_array() perl 的一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45335034/
嗨! 我想一切都在问题中......我只是想知道 Python 中是否有缩短此模式的好方法: something = get_something() if something: do_a_th
我知道: $sth->fetchrow_hashref返回从数据库中获取的行的 hashref, $sth->fetchrow_arrayref返回从数据库中获取的行的数组引用,和 $sth->fet
我有这段代码: public class ListCollection implements Iterable { private int size; private ListNode
嗨 我有一个关于如何仅获取给定字符串的一部分的问题: String = "1. Name:Tom\tNumber:123"; 在这种情况下,我只想获取名称为“Tom”的部分 有什么办法可以解决吗? 感
这两个查询的结果怎么可能: SELECT * FROM `workers` WHERE `name` = 'Smith` 和 SELECT * FROM `workers` WHERE NOT `na
有什么区别 request.POST.get('sth') 和 request.POST['sth'] 没有找到类似的问题,两者对我来说都一样,假设我可以单独使用它们但也许我错了,这就是我问的原因。有
现在我在表单中提交帖子数据时遇到问题(我的表单如下所示: Task: Category: DueDate: )提交表单后,我会收到此错误: Found entity of type Doctr
假设我有两个输入字段,输入字段 A 和输入字段 B。我想在输入字段 A 中输入内容,但同时输入字段 A 中的值也会进入输入字段 B。 我知道这个方法可以通过 inputfiledB.value() =
我在 main.c 中有以下代码 #include "config.h" #include "util.h" 并且在config.h #include "util.h" 并且util.h中有一个正常的
只是想知道是否有人可以启发我sth 可能是什么。 (在 Transanalyzer 流文件中看到。)基本上它是一个网络分析类别(IP 地址、端口、sth 等),但我不确定它的含义以及那里文档中没有提及
这是我的代码: . . keydown: function(ev) { clearTimeout( $(this).data('timer') ); if ( 'abort' in $
我正在 Java 中寻找一个健壮的 Map,其中键查找将考虑到 Double 的精度有限(大约 1e-15 或 1e-16)。我在哪里可以找到这样的东西? 编辑:根据 Jon 的建议,我认为定义等价性
我想通过此路由通过 ID 查看我的用户: path('users/', views.UserDetail.as_view()), 在我看来,我有这样的代码: users.objects.get(pk
我的 python 程序接受来自命令行参数 (sys.argv[1]) 和管道 (sys.stdin) 的主要输入。 但它需要额外的参数,当主输入不存在时,这些参数将改变它们在 sys.argv 列表
我正在阅读一个 jquery 插件,发现这个有趣的语法: 'sth'+ +new Date 它创建一个数字字符串,作者将其用作唯一 ID:sth1237004731731 我很好奇它是什么样的语法,是
在 PDO 示例中,我经常发现使用了首字母缩略词 $dbh 和 $sth。我想 $dbh 代表“数据库句柄”——对吗? $sth 怎么样? “语句句柄”? 是否有充分的理由使用上述而不是 $db_co
我正在尝试学习 F#(我是 C# 开发人员),并在尝试编译以下代码时遇到了我的第一个问题: let decompose n = seq{ let mutabl
我想知道这种构造函数链调用在java中是否可行?我正在扩展 JButton 基类,我需要首先初始化 super 变量,然后使用默认构造函数初始化我的类。 public CustomButton(){
sub completecheckout { $cryptedcard = md5_hex($cardnum . $salt); $grabcart = qq~select pid f
我将自己的标签转换为模板 text 我需要在两个输入中具有相同的值。 plunker 如果我将范围从范围:{value:'@'}固定为范围:false,则它有效, 但是 我无法触及范围 {value:
我是一名优秀的程序员,十分优秀!