我有一个脚本,它发送短信给所有成员。首先,当我发送短信时,我存储在MySQL数据库中,然后从不同的脚本中获取相同的内容,并批量发送给我的短信网关提供商。
短信调度表有如下列:
`id, to, senderid, message, unicode, status`
Unicode有3个值:1、2、3
1 = english
2 = unicode
3 = flash
然后在PHP中像这样运行查询
$sql = "SELECT `to`, `senderid`, `message` FROM `sch_sms` WHERE `status`='0' AND `unicode` = '1' LIMIT 1000
然后我创建for loop
并使用cron发送短信。
目前,我可能不得不编写上述sql查询3次,并运行3次不同的基于unicode条件发送短信。
我想知道的是,是否有可能对所有3个不同的unicode值和单个for loop
进行单个查询,然后处理我的php sms函数。
使用IN
查看代码是否在列中:
SELECT `to`, `senderid`, `message`, `unicode`
FROM `sch_sms`
WHERE `status`='0'
AND `unicode` IN('1','2','3')
LIMIT 1000
如果这是该列中仅有的三个值,则可以完全取消条件:
SELECT `to`, `senderid`, `message`, `unicode`
FROM `sch_sms`
WHERE `status`='0'
LIMIT 1000
完成上述操作后,您可以设置一个while循环,在其中您可以使用switch语句来确定消息应该如何发送(伪代码)-
while($row = <appropriate database function here>) {
switch ($row['unicode']) {
case 1:
// code for this send type
break;
case 2:
// code for this send type
break;
case 3:
// code for this send type
break;
}
为了说明我所说的
是什么意思如果您必须将它们"批量"发送到您的短信网关,请使用ORDER BY语句并检查当前记录是否与最后一个记录具有相同的unicode值;如果不是……这是下一组。
(不确定你是否真的需要这个....)
<?php
$pdo = new PDO('mysql:host=localhost;dbname=test;charset=utf8', 'localonly', 'localonly', array(
PDO::MYSQL_ATTR_DIRECT_QUERY => false,
PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION
));
setup($pdo);
$currentUnicode = null;
foreach( $pdo->query('SELECT unicode, message FROM soFoo WHERE status=0 ORDER BY unicode', PDO::FETCH_ASSOC) as $row ) {
// check if this message is the first of a new unicode-group
if ( $row['unicode']!==$currentUnicode ) {
switchCode($row['unicode']);
// it's the new "current" group
$currentUnicode=$row['unicode'];
}
sendSMS($row);
}
function switchCode($newCode) {
echo "--- switching to new Code: $newCode ---'r'n";
}
function sendSMS($data) {
echo "sending msg: {$data['message']}'r'n";
}
// boilerplate, creates temp table and inserts sample data....
function setup($pdo) {
$pdo->exec('
CREATE TEMPORARY TABLE soFoo (
id int auto_increment,
message varchar(32),
unicode int,
status int,
primary key(id)
)
');
$stmt = $pdo->prepare('INSERT INTO soFoo (message,unicode, status) VALUES (?,?,0)');
$stmt->bindParam(1, $message);
$stmt->bindParam(2, $unicode);
foreach( range('a','k') as $c ) {
foreach(range(1,3) as $unicode) {
$message = sprintf('msg%d%s', $unicode, $c);
$stmt->execute();
}
}
}
打印
--- switching to new Code: 1 ---
sending msg: msg1a
sending msg: msg1g
sending msg: msg1e
sending msg: msg1h
sending msg: msg1d
sending msg: msg1i
sending msg: msg1c
sending msg: msg1j
sending msg: msg1b
sending msg: msg1k
sending msg: msg1f
--- switching to new Code: 2 ---
sending msg: msg2i
sending msg: msg2j
sending msg: msg2h
sending msg: msg2g
sending msg: msg2k
sending msg: msg2f
sending msg: msg2c
sending msg: msg2a
sending msg: msg2e
sending msg: msg2d
sending msg: msg2b
--- switching to new Code: 3 ---
sending msg: msg3a
sending msg: msg3j
sending msg: msg3b
sending msg: msg3i
sending msg: msg3f
sending msg: msg3c
sending msg: msg3h
sending msg: msg3d
sending msg: msg3g
sending msg: msg3e
sending msg: msg3k
switchCode()是让你的短信网关知道切换unicode的函数。
编辑:
在伪代码中,您的工作流可能看起来像
if ( changed(unicode) or changed(message) or toolong($to+row['to']) ) {
send current sms request to gateway
reset unicode, message, to
}
else {
concat $to $row['to']
}
这是偏离主题,但你可能想要考虑一个不同的表布局,例如
CREATE TABLE smsUsers (
user_id int auto_increment,
user_unicode int,
user_phonenumber ...,
...
)
CREATE TABLE smsMessages (
message_id int auto_increment,
message varchar(32),
...
)
CREATE TEMPORARY TABLE smsMessageQueue (
queue_id int auto_increment,
message_id int,
user_id int,
queue_status int,
...
)
这样至少可以降低识别不同消息文本的成本,并可能节省数据库中的一些空间。