我正在尝试将两个电子邮件地址传递给MySQL以查找其帐户。我这样称呼我的剧本:
./message-2.pl user1@s 'rech user2@s 22'
这是我的剧本:
#!/usr/bin/perl
use warnings;
use strict;
use DBI;
use IO::Handle;
our $calerid = "'$ARGV[0]'";
our $message = "$ARGV[1]";
our $msg_arg1 = qx(echo $message | awk '{print \$1}');
our $msg_arg2 = qx(echo $message | awk '{print \$2}');
my $share_reciver = "'$msg_arg2'";
my $database = "mya2billing";
my $host = "localhost";
my $port = "3306";
my $user = "root";
my $pass = "xxxxxx";
my $dsn = "DBI:mysql:database=$database;host=$host;port=$port";
my $sth;
# find card id
my $query1 = "SELECT id_cc_card FROM cc_callerid WHERE cid = $calerid ;";
my $dbh = DBI->connect($dsn, $user, $pass) || die "Could not connect to database:$DBI::errstr";
our $card;
print " query=$query1\n";
if (!($sth = $dbh->prepare($query1))) {
die ("Failed to prepare statement: " . DBI->errstr);
}
$sth->execute() or die $DBI::errstr;
my @result;
while (@result = $sth->fetchrow_array) { # retrieve one row
($card) = @result;
print "card id found: $card \n";
}
if ($msg_arg1 =~ "rech" or $msg_arg1 =~ "Rech" or $msg_arg1 =~ "bal" or $msg_arg1 =~ "Bal") {
recharge();
}
sub recharge {
print " we are in rech section\n";
our $msg_arg2 = qx(echo $message | awk '{print \$2}');
our $share_reciver="$msg_arg2";
my $query2 = "SELECT id_cc_card FROM cc_callerid WHERE cid = '$share_reciver' ";
my $dbh = DBI->connect($dsn, $user, $pass);
$sth = $dbh->prepare($query2);
$sth->execute() or die $DBI::errstr;
print " query=$query2\n";
my $receiver_id;
while (my @row = $sth->fetchrow_array) {
($receiver_id) = @row;
print " we are in query section\n";
print "receiver_cardid:$receiver_id \n";
}
exit 1;
}
这是输出:
[root@laptop Desktop]# ./message-2.pl user1@s 'rech user2@s 22'
query=SELECT id_cc_card FROM cc_callerid WHERE cid = 'user1@s' ;
card id found: 10
we are in rech section
query=SELECT id_cc_card FROM cc_callerid WHERE cid = 'user2@s
'
没有错误。 query1工作正常但query2无法正常工作。我试图使用占位符而不是$share_reciver
,但它不起作用。我也试过单引号而没有引号。如果我使用$calerid
代替$share_reciver
,那么它可以正常工作。谁能告诉我我错过了什么?