我在编码的各个方面都是新手,请多多包涵。
我已经创建了一个与我的 PHP 应用程序通信的 Perl 脚本,但在将它们与 MySQL 查询同步方面遇到了最后的障碍。我希望 Perl 脚本在我的数据库中读取表 Syncorderrelation
,并且只自动响应一次,通过在我的“外发表”上发布“自动响应”来发送文本消息)到任何新条目满足以下条件:
updatetype
为 1 的条目,这意味着新条目是订阅产品的新个人。Perl 脚本读取
DEST_MSISDN
的新条目,这是同一个Syncorderrelation
表中的一个字段(只是一个电话号码)并回复带有自动回复的号码(只是一条欢迎消息)。这是通过仅在“传出表”上发布“自动响应”来完成的,我的 PHP 应用程序会在其中接管并自行发送响应。
所以我的问题是:我如何着手创建一个查询,Perl 脚本将自动回复从当前时间开始的 Syncorderrelation
表中的新条目。目前,还有数百个我不想触发响应的其他条目。
但是只回复新的 DEST_MSISDN
(电话号码)并且只回复这个电话号码并且只回复一次,因此不会继续向同一个号码重新发送同一个欢迎消息一次又一次。
注意:脚本知道这是一个新条目的唯一方法是通过 ID
字段。这是一个独特的字段,每次有新条目进入 Syncorderrelation
表时都会自动生成(该表现在已从当前的“440”增加到“441”)。
我创建的脚本正确地将所有必需字段从 Syncoderrelation
表发送到 Outgoing
表,但无法读取新的 Dest_Msisdn
(电话号码)从 Syncorderrelation
表并将电话号码发布到 Outgoing
表。没有它,PHP 应用程序将不知道将欢迎消息发送到哪里。
use strict;
use LWP::UserAgent;
use HTTP::Request::Common qw(POST);
use XML::Simple;
use Digest::MD5 qw(md5_hex);
use Time::Piece;
use DBI;
use Time::HiRes qw(usleep nanosleep);
my $dest_msisdn = '';
my $link_id = '';
my $ID = '';
my $subscription_auto_reply = '';
my $user = '';
my $send_time = '';
my $now = '';
my $correlator = '2345';
my $sender_name = '30045';
my $source = '30045';
my $text_message = 'Dear Customer Welcome to our service';
my $ua = new LWP::UserAgent(keep_alive => 1);
#get all the configs
#get sms to be sent
#set the connection
my $dbh = DBI->connect('DBI:mysql:Database404', 'Kapo', 'XXXXXXXXX') || die "Could not connect to database: $DBI::errstr";
my $j = 0;
while ($j < 1) {
print "AutoResponse....\n";
my $sql = "SELECT * FROM syncorderrelation WHERE updatetype = '1'";
my $results = $dbh->selectall_hashref($sql, 'id');
foreach my $id (keys %$results) {
print "AutoResponder!!!.....\n";
#get all the required columns
#get all the required columns
my $textMessage = $results->{$id}->{text_message}; #$results->{$id}->{dest_msisdn};
my $correlator = $results->{$id}->{id};
my $senderName = $results->{$id}->{sender_name};
my $linkID = $results->{$id}->{link_id};
my $textID = $results->{$id}->{id};
my $client = $results->{$id}->{client};
my $productId = $results->{$id}->{product_id};
my $dest_msisdn = $results->{$id}->{dest_msisdn};
my $text_message = $results->{$id}->{text_message};
my $autoReply = $results->{$id}->{autoreply};
#####send service
my $user = 5;
my $send_time = time;
my $now = time;
#catch error
my $sql1 = "INSERT INTO outgoing_sms(dest_msisdn, text_message, sender_name, link_id, correlator, send_time, client, user_id, timestamp) VALUES ('$dest_msisdn', 'Dear Customer Welcome to our service', '$sender_name', '$link_id', '1161', '$now', '1', '$user', '$now')";
$dbh->do($sql1);
#print $sql1;
###send
print $sql1;
#remove from outgoing table
my $sql2 = "UPDATE incoming_sms SET sent_flag = 1 WHERE id='$correlator'";
$dbh->do($sql2);
print $sql2;
}
usleep(1000000);
}
最佳答案
我对你的代码有很多观察
始终
使用警告
以及使用严格
不要在程序的顶部声明所有变量。它们应该尽可能靠近它们的第一个使用点声明
对于许多 不必要的模块和许多 未使用或两次声明的变量,您有一个
use
。在寻求帮助之前整理您的代码只是礼貌。使用整洁的程序对您来说也容易得多,但那是您自己的事。类似地,不要写注释,除非您必须实现复杂到难以理解的代码。并且说一些代码是如何工作的,而不是它做了什么:在调用
DBI->connect
之前评论set the connection
是荒谬的在大多数情况下,您应该使用 DBI 的
prepare
和execute
而不是do
。应该使用占位符传递参数,而不是将它们插入到 SQL 字符串中最好询问数据库它认为现在是什么时间,而不是使用本地系统时间。这样就可以正确比较数据库中不同表的时间戳
您应该将对多个表的相关更改放在一个事务中,这样,在发生错误的情况下,一个更改不能没有另一个更改
据我所知,您正在寻找 syncorderrelation
中 updatetype
为 1 且 incoming_sms
中相应行的所有条目> sent_flag
not 设置为 1。为此,您需要对两个表进行 JOIN
。
代码应该是这样的。 (参见 - 一个模块,更少的变量声明!)但显然我无法在没有访问测试数据的情况下检查它。
use strict;
use warnings;
use DBI;
my $dbh = DBI->connect('DBI:mysql:Database404', 'Kapo', 'XXXXXXXXX')
or die "Could not connect to database: $DBI::errstr";
my $get_new_ids = $dbh->prepare(<<__ENDSQL__);
SELECT syncorderrelation.* FROM syncorderrelation INNER JOIN incoming_sms
ON incoming_sms.id = syncorderrelation.id
WHERE syncorderrelation.updatetype = 1 AND incoming_sms.sent_flag != 1
__ENDSQL__
my $insert_outgoing = $dbh->prepare(<<__ENDSQL__);
INSERT INTO outgoing_sms (
dest_msisdn,
text_message,
sender_name,
link_id,
correlator,
send_time,
client,
user_id,
timestamp)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
__ENDSQL__
my $update_incoming = $dbh->prepare('UPDATE incoming_sms SET sent_flag = 1 WHERE id = ?');
while () {
while (my $data = $get_new_ids->fetchrow_hashref) {
my ($now) = $dbh->selectrow_array('SELECT CURRENT_TIMESTAMP');
$dbh->do('START TRANSACTION');
$insert_outgoing->execute(
$data->{dest_msisdn}, # dest_msisdn,
'Dear Customer Welcome to our service', # text_message,
$data->{sender_name}, # sender_name,
$data->{link_id}, # link_id,
1161, # correlator,
$now, # send_time,
1, # client,
5, # user_id,
$now, # timestamp
);
$update_incoming->execute($data->{id});
$dbh->do('COMMIT');
}
sleep 2;
}
关于php - 如何将我的 Perl 脚本与 MySQL 查询同步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21721569/