A
A
Artem Lungu2021-09-11 00:15:14
PHP
Artem Lungu, 2021-09-11 00:15:14

How to do sequential output from MySQL?

Good to everyone! In general, this is the problem. It is necessary to display records from the database and send all this data one by one to a third-party server. those. in the database, for example, there are 500 numbers and you need to transfer each number to the server one by one. if you do it like this:

$res = mysql_result(mysql_query("SELECT COUNT(*) FROM `". input($pages) ."` ". $sql_where .""), 0);
          if ($res > 0) {
          $req = mysql_query("SELECT * FROM `". input($pages) ."` ". $sql_where ."");
          // URL для запроса POST /message
$token = 'token ';
$instanceId = '123';
$url = 'https://sitе.com/instance'.$instanceId.'/message?token='.$token;
          while ($data_req = mysql_fetch_assoc($req)) {
            
    $string = str_replace("+7","",$data_req[$data_tel_res]);	
  $string = str_replace("(","",$string);	
  $string = str_replace(")","",$string);	
  $string = str_replace("-","",$string);	
  $string = str_replace(" ","",$string);
  $string = substr_replace($string, '', 0, 1);
        # echo $string .'<br/>';
          $data = [
    'phone' => '7'. $string, // Телефон получателя
    'body' => $text, // Сообщение
];
$json = json_encode($data); // Закодируем данные в JSON
// Сформируем контекст обычного POST-запроса
$options = stream_context_create(['http' => [
        'method'  => 'POST',
        'header'  => 'Content-type: application/json',
        'content' => $json
    ]
]);
// Отправим запрос
$result = file_get_contents($url, false, $options);

          }
          
          if ($result) {
            
            $error_404 = 'Отправленно '. $res .' сообщений!<br/>';
          } else {
            $error_404 = 'Ошибка отправки '. $res .' сообщений!<br/>';
          }
          
          } else {$error_404 = 'На странице '. $pages .' нет номеров!<br/>';}

that at sampling in 100 records, sat hangs and sometimes produces an error on time.
You can somehow do this one by one or synchronously (asynchronously or whatever it is right). Maybe there is a library with js somewhere? I saw this in PHPMailer, there, when sending, a window pops up and through js how much is sent and in the queue and sent. plus you can pause on the button. I would like to come up with something like this with the old code.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
dzmitryIhnatau, 2021-09-11
@dzmitryIhnatau

This is where ajax comes in handy.
The logic is this:
First you get the number of all records in the database. Then you send it all in portions via ajax while using LIMIT in the sql query. Try to start sending 20 records at a time.

T
ThunderCat, 2021-09-11
@ThunderCat

There are a lot of options, the simplest thing that comes to mind is to run the script from the console, there are no time limits and you can send as much as you like.
The second option may not be suitable or not provided by the receiving server. The request is similar to an api call, most likely it has the ability to accept several values ​​at once in a certain format, but this is not certain ...
The third option is to increase the maximum script running time through the ini_set.
Well, and a bunch of other options: multicurl, rabbitMKU, sending by krone...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question