简体   繁体   English

根据PHP中的列条件处理MySQL查询

[英]Process MySQL query based on column conditions in PHP

I have a script where it sends out the sms to all members. 我有一个脚本,它向所有成员发送短信。 First, when I send out the SMS, I store in MySQL database and then fetch the same from different script and send batchwise to my SMS gateway provider. 首先,当我发送SMS时,我将其存储在MySQL数据库中,然后从不同的脚本中获取相同的内容,然后批量发送给我的SMS网关提供程序。

Schedule sms table has following columns. Schedule sms表具有以下列。

`id, to, senderid, message, unicode, status`

Unicode has 3 values as 1,2,3 Unicode具有3个值为1,2,3

1 = english
2 = unicode
3 = flash

Then I run the query like this in PHP 然后我在PHP中像这样运行查询

 $sql = "SELECT `to`, `senderid`, `message` FROM `sch_sms` WHERE `status`='0' AND `unicode` = '1' LIMIT 1000

Then I create for loop and send out the sms using cron. 然后,我创建for loop并使用cron发送短信。

Currently I might have to write the above sql query 3 times and run 3 times differently to send sms based on unicode conditions. 目前,我可能必须编写3次以上sql查询,并分别运行3次才能根据unicode条件发送短信。

What I would like to know is, is it possible to make a single query for all 3 different unicode values and also single for loop then process my php sms function. 我想知道的是,是否可以对所有3个不同的unicode值进行单个查询,也可以for loop进行单个查询,然后处理我的php sms函数。

Any advice would be much appreciated. 任何建议将不胜感激。

Use IN to see if the code is in the column: 使用IN查看代码是否在列中:

SELECT `to`, `senderid`, `message`, `unicode` 
FROM `sch_sms` 
WHERE `status`='0' 
AND `unicode` IN('1','2','3') 
LIMIT 1000

If those are the only three values in that column you can drop the condition entirely: 如果这些是该列中仅有的三个值,则可以完全删除条件:

SELECT `to`, `senderid`, `message`, `unicode`
FROM `sch_sms` 
WHERE `status`='0' 
LIMIT 1000

Having done the above you can then setup a while loop where you can use a switch statement to determine how the message should be sent (pseudo-code) - 完成上述操作后,您可以设置一个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;
}

Just as an illustration of what I meant by 只是说明我的意思

If you have to send them "bulk-wise" to your sms gateway use an ORDER BY statement and check if the current record has the same unicode value as the last one; 如果必须将它们“批量”发送到您的短信网关,请使用ORDER BY语句,并检查当前记录的unicode值是否与最后一个相同。 if not ...it's the next group. 如果不是,那是下一组。

(not sure if you really need this though ....) (虽然不确定您是否真的需要这个……)

<?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();
        }
    }
}

prints 版画

--- 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() would be the function to let your sms gateway know to switch the unicode. switchCode()是让您的短信网关知道切换Unicode的功能。

edit: 编辑:
in pseude-code your workflow might look something like 用伪代码,您的工作流程可能看起来像

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']
}

This is going offtopic but you might want to consider a different table layout like eg 这已经不合时宜了,但是您可能需要考虑其他表格布局,例如

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,
    ...
)

at the very least it would be less costly to discern different message texts and probably saves some space in the database. 至少,辨别不同的消息文本会节省成本,并且可能节省数据库空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM