Core: How to get, edit and add Database datas in Commands

Created on 8 Apr 2017  路  6Comments  路  Source: php-telegram-bot/core

I am trying to add my external class to get, edit and add Database datas in Commands.

But that doesnt go, know i want to know:
The SDK allready have an mysql option integrated, i added details in hook.php.

And now i need to grab Database details for some Commands.
Like:
User sends Command /Points -> SDK connects to Database & Grabs his amount of Points -> Reply: You have 10 Points.

Kind Regards & with <3
Raphael

Most helpful comment

Untested, should show you the right way.

<?php

namespace Longman\TelegramBot\Commands\UserCommands;

use Longman\TelegramBot\Commands\UserCommand;
use Longman\TelegramBot\Request;
use Longman\TelegramBot\DB;

class PointsCommand extends UserCommand
{
    protected $name = 'points';

    public function execute()
    {
    $message = $this->getMessage();
    $message_id = $message->getMessageId();
    $chat_id = $message->getChat()->getId();
    $user_id = $message->getFrom()->getId();

    $points = 0;
    if ($result = DB::getPdo()->query('SELECT * FROM `points` WHERE `user_id` = ' . $user_id)) {
        $result = $result->fetchAll();
        $points = $result[0]['count'];
    }

    $data = [];
    $data['chat_id'] = $chat_id;
    $data['reply_to_message_id'] = $message_id;
    $data['parse_mode'] = 'markdown';
    $data['text'] = 'You have *' . $points . '* points.';

    return Request::sendMessage($data);
    }
}

Output: "You have 234 points."

This assumes the field containing the number of points is called count and user_id is unique field for table points.

All 6 comments

DB::getPdo() allows you to grab ready to use PDO object.

Could you make me please an example i dont understand right know ;(

Untested, should show you the right way.

<?php

namespace Longman\TelegramBot\Commands\UserCommands;

use Longman\TelegramBot\Commands\UserCommand;
use Longman\TelegramBot\Request;
use Longman\TelegramBot\DB;

class PointsCommand extends UserCommand
{
    protected $name = 'points';

    public function execute()
    {
    $message = $this->getMessage();
    $message_id = $message->getMessageId();
    $chat_id = $message->getChat()->getId();
    $user_id = $message->getFrom()->getId();

    $points = 0;
    if ($result = DB::getPdo()->query('SELECT * FROM `points` WHERE `user_id` = ' . $user_id)) {
        $result = $result->fetchAll();
        $points = $result[0]['count'];
    }

    $data = [];
    $data['chat_id'] = $chat_id;
    $data['reply_to_message_id'] = $message_id;
    $data['parse_mode'] = 'markdown';
    $data['text'] = 'You have *' . $points . '* points.';

    return Request::sendMessage($data);
    }
}

Output: "You have 234 points."

This assumes the field containing the number of points is called count and user_id is unique field for table points.

Thanks, okey that is how i grab something, but how cann i update points to user in db or how can i insert a table with the SDK ?

Sry for that Questions but i tried it and it is frustrating.

It worked ;9 Thanks for every help

Just google up a for PDO tutorials, it's a PHP feature and there is a lot about it in the web.

You can see how records are added/updated in DB.php in the library.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Bl0ck154 picture Bl0ck154  路  3Comments

smaznet picture smaznet  路  4Comments

sayjeyhi picture sayjeyhi  路  3Comments

dorcu picture dorcu  路  3Comments

Recouse picture Recouse  路  3Comments