P
P
pimanov32019-04-22 16:46:27
PHP
pimanov3, 2019-04-22 16:46:27

How to fix the code or change the implementation?

I'm making a bot in VK from flooders in a conversation. Here is what is there, but there is no result. Everything related to the exclusion of the user, the connection to the database - it works, there are no errors during execution either. The table has columns Id (primary key), user_id, time (number of seconds that have passed since the beginning of the century).
If there is some other problem with all this, can you advise another way to implement it?

<?php
require_once('vk.php');

$vk = new VK();

    $peer_id1 = $vk->data['object']['peer_id'];
    $peer_id = $vk->data['object']['peer_id']; 
    $text = $vk->data['object']['text'];
    $user_link_b = $vk->data['object']['action']['type'];
    $user_id = $vk->data['object']['from_id'];
    $member_id = $vk->data['object']['action']['member_id'];
    $chat_id = '2';
    // Параметры для подключения
    $db_host = "localhost"; 
    $db_user = "123"; // Логин БД
    $db_password = "pas"; // Пароль БД
    $db_base = 'bd'; // Имя БД
    $db_table = "table"; // Имя Таблицы БД
    
// Подключение к базе данных
$mysqli = new mysqli($db_host,$db_user,$db_password,$db_base);

// Если есть ошибка соединения, выводим её и убиваем подключение
if ($mysqli->connect_error) {
die('Ошибка : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
}
    
if ($vk->data['type'] == 'message_new')
{
// Добавление
$time_s = date("U");
$result = $mysqli->query("INSERT INTO ".$db_table." (user_id,time) VALUES ('$user_id','$time_s')");

// Id
$lim = $mysqli->query("SELECT MAX(Id) FROM table");
$row = $lim->fetch_row();
if(($row[0] % 3) == 0)
{
// time и user_id
$time111 =$mysqli->query("SELECT time FROM table ORDER BY id LIMIT 1 OFFSET 0"); // time - 1
$time11 = $time111->fetch_row();
$time1 = $time11[0];

$time222 =$mysqli->query("SELECT time FROM table ORDER BY id LIMIT 1 OFFSET 1"); // time - 2
$time2 = $time222->fetch_row();
$time2 = $time22[0];

$time333 =$mysqli->query("SELECT time FROM table ORDER BY id LIMIT 2 OFFSET 2"); // time - 3
$time3 = $time333->fetch_row();
$time3 = $time33[0];

$user111 =$mysqli->query("SELECT user_id FROM table ORDER BY id LIMIT 1 OFFSET 0"); // user_id - 1
$user11 = $user111->fetch_row();
$user1 = $user11[0];

$user222 =$mysqli->query("SELECT user_id FROM table ORDER BY id LIMIT 1 OFFSET 1"); // user_id - 2
$user22 = $user222->fetch_row();
$user2 = $user22[0];

$user333 =$mysqli->query("SELECT user_id FROM table ORDER BY id LIMIT 2 OFFSET 2"); // user_id - 3
$user33 = $user333->fetch_row();
$user3 = $user33[0];

// Проверка
if((($time3 - 2) >= $time1) and (($time3 - 2) >= $time2) and ($user3 == $user1) and ($user3 == $user2))
{
// Исключение пользователя
$vk->send1($chat_id, $user_id, $member_id);
}

// Удаляет все записи из таблицы
$mysqli->query("truncate table table");
}
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stalker_RED, 2019-04-24
@pimanov3

Let me finally turn on the telepathy mode and try to guess what you wanted to do.
You write the user_id and the time of all messages to the table
(you did it)
Next, you want to compare the last three (?) records and check if there are any duplicate user_ids among them for the last two seconds
(instead, you select data for three records from the BEGINNING of the table and you do it for some reason with six requests)
How can I fix it?
Select only entries in the last two seconds
Moreover, you can immediately select only those users who are repeated!

select user_id ftom myTable
where time > now() - interval 2 second
group by user_id
having COUNT(*) > 1

Even cooler - you can not torment the database at all, but simply store an array in memory where the key is the time, and the user_id is the value. Throw out entries older than X seconds and look for duplicate user_ids.
Save on load by several orders of magnitude.

A
Alexey Nikolaev, 2019-04-22
@Heian

Kill it with fire and write normally. Well, if time permits. Although it seems to me that you are digging into this no less number of hours than you would spend writing from scratch.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question