Longman\TelegramBot\DB::selectMessages PHP Method

selectMessages() public static method

Fetch message(s) from DB
public static selectMessages ( integer $limit = null ) : array | boolean
$limit integer Limit the number of messages to fetch
return array | boolean Fetched data or false if not connected
    public static function selectMessages($limit = null)
    {
        if (!self::isDbConnected()) {
            return false;
        }
        try {
            $sql = '
                SELECT *
                FROM `' . TB_MESSAGE . '`
                WHERE `update_id` != 0
                ORDER BY `message_id` DESC
            ';
            if ($limit !== null) {
                $sql .= 'LIMIT :limit';
            }
            $sth = self::$pdo->prepare($sql);
            $sth->bindParam(':limit', $limit, PDO::PARAM_INT);
            $sth->execute();
            return $sth->fetchAll(PDO::FETCH_ASSOC);
        } catch (PDOException $e) {
            throw new TelegramException($e->getMessage());
        }
    }

Usage Example

 /**
  * Handle getUpdates method
  *
  * @return \Longman\TelegramBot\Telegram
  */
 public function handleGetUpdates($limit = null, $timeout = null)
 {
     //DB Query
     $last_message = DB::selectMessages(1);
     if (isset($last_message[0]['update_id'])) {
         //As explained in the telegram bot api documentation
         $offset = $last_message[0]['update_id'] + 1;
     } else {
         $offset = null;
     }
     //arrive a server Response object
     $ServerResponse = Request::getUpdates(['offset' => $offset, 'limit' => $limit, 'timeout' => $timeout]);
     if ($ServerResponse->isOk()) {
         $results = '';
         $n_update = count($ServerResponse->getResult());
         for ($a = 0; $a < $n_update; $a++) {
             $result = $this->processUpdate($ServerResponse->getResult()[$a]);
         }
         print date('Y-m-d H:i:s', time()) . ' - Processed ' . $a . " updates\n";
     } else {
         print date('Y-m-d H:i:s', time()) . " - Fail fetch updates\n";
         echo $ServerResponse->printError() . "\n";
     }
     //return $results
 }
All Usage Examples Of Longman\TelegramBot\DB::selectMessages