Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使该算法更有效_Php - Fatal编程技术网

Php 使该算法更有效

Php 使该算法更有效,php,Php,我是个php高手,需要帮助。我已经建立了一个推送服务,向iphone发送推送消息。它在评论中说,将多条消息组合成一个数据包更有效。而且,现在我打电话删除每一条信息。如何将多条消息合并到一个包中 谢谢 function start() { writeToLog('Connecting to ' . $this->server); if (!$this->connectToAPNS()) exit; while (true) {

我是个php高手,需要帮助。我已经建立了一个推送服务,向iphone发送推送消息。它在评论中说,将多条消息组合成一个数据包更有效。而且,现在我打电话删除每一条信息。如何将多条消息合并到一个包中

谢谢

function start()
{
    writeToLog('Connecting to ' . $this->server);

    if (!$this->connectToAPNS())
        exit;


    while (true)
    {
        // Do at most 20 messages at a time. Note: we send each message in
        // a separate packet to APNS. It would be more efficient if we 
        // combined several messages into one packet, but this script isn't
        // smart enough to do that. ;-)

        $stmt = $this->pdo->prepare('SELECT * FROM push_queue WHERE time_sent IS NULL LIMIT 20');
        $stmt->execute();
        $messages = $stmt->fetchAll(PDO::FETCH_OBJ);

        foreach ($messages as $message)
        {
            if ($this->sendNotification($message->message_id, $message->device_token, $message->payload))
            {
                //$stmt = $this->pdo->prepare('UPDATE push_queue SET time_sent = NOW() WHERE message_id = ?');
                //$stmt->execute(array($message->message_id));

                $stmt = $this->pdo->prepare('DELETE FROM push_queue WHERE message_id = ?');
                $stmt->execute(array($message->message_id));

            }
            else  // failed to deliver
            {
                $this->reconnectToAPNS();
            }
        }

        unset($messages);           
        sleep(5);
    }
}

您可以在阵列中对已删除的邮件ID进行分组

$deletedIds = array()
foreach ($messages as $message)
{
    if ($this->sendNotification())
    {
        $deletedIds[] = $message->message_id;
    }
}
之后,您可以使用查询方法删除这些消息

$this->pdo->query('DELETE FROM push_queue WHERE message_id IN ('.implode(',', $deletedIds).')');

您可以在阵列中对已删除的邮件ID进行分组

$deletedIds = array()
foreach ($messages as $message)
{
    if ($this->sendNotification())
    {
        $deletedIds[] = $message->message_id;
    }
}
之后,您可以使用查询方法删除这些消息

$this->pdo->query('DELETE FROM push_queue WHERE message_id IN ('.implode(',', $deletedIds).')');

您的问题与优化SQL无关,而是关于如何处理SQL的结果。。。你需要更详细地解释这一部分,才能得到有用的答案!您的问题与优化SQL无关,而是关于如何处理SQL的结果。。。你需要更详细地解释这一部分,才能得到有用的答案!谢谢效果很好。是否可以对正在发送的消息查询执行类似的操作?谢谢。效果很好。是否可以对正在发送的消息查询执行类似的操作?