PHP.mk документација

mysqli::poll

Почист и полокален преглед на PHP референцата, со задржана структура од PHP.net и подобра читливост за примери, секции и белешки.

mysqli.poll.php PHP.net прокси Преводот се освежува
Оригинал на PHP.net
Патека mysqli.poll.php Локална патека за оваа страница.
Извор php.net/manual/en Оригиналниот HTML се реупотребува и локално се стилизира.
Режим Прокси + превод во позадина Кодовите, табелите и белешките остануваат читливи во истиот тек.
mysqli::poll

Референца за `mysqli.poll.php` со подобрена типографија и навигација.

mysqli.poll.php

mysqli::poll

mysqli_poll

(PHP 5 >= 5.3.0, PHP 7, PHP 8)

mysqli::poll -- mysqli_poll(PHP 5 >= 5.3.0, PHP 7, PHP 8)

= NULL

Напиши целосна ознака на елемент

public static mysqli::poll(
         ?array &$read,
         ?array &$error,
         array &$reject,
         int $seconds,
         int $microseconds = 0
): int|false

Процедурален стил

mysqli_poll(
         ?array &$read,
         ?array &$error,
         array &$reject,
         int $seconds,
         int $microseconds = 0
): int|false

Проверка на конекции static.

Забелешка:

Достапно само со mysqlnd.

Параметри

read

Проверка на конекции. Методот може да се користи како

error

Список на конекции за проверка на извонредни резултати што можат да се прочитаат.

reject

Список на конекции на кои се појави грешка, на пример, неуспех на прашањето или изгубена конекција.

seconds

Список на конекции отфрлени бидејќи не е извршено асинхроно прашање на кое функцијата може да врати резултати.

microseconds

Максимален број на секунди за чекање, мора да биде не-негативен.

Вратени вредности

Максимален број на микросекунди за чекање, мора да биде не-негативен. false otherwise.

Errors/Exceptions

А ValueError Враќа број на подготвени конекции при успех, read nor error се фрла кога ниту еден од

Дневник на промени

Верзија = NULL
8.3.0 Сега фрла ValueError аргументи се поминуваат. read nor error се фрла кога ниту еден од

Примери

ако е овозможен колекторот за отпадоци, mysqli_poll() example

<?php
$link1
= mysqli_connect();
$link1->query("SELECT 'test'", MYSQLI_ASYNC);
$all_links = array($link1);
$processed = 0;
do {
$links = $errors = $reject = array();
foreach (
$all_links as $link) {
$links[] = $errors[] = $reject[] = $link;
}
if (!
mysqli_poll($links, $errors, $reject, 1)) {
continue;
}
foreach (
$links as $link) {
if (
$result = $link->reap_async_query()) {
print_r($result->fetch_row());
if (
is_object($result))
mysqli_free_result($result);
} else die(
sprintf("MySQLi Error: %s", mysqli_error($link)));
$processed++;
}
} while (
$processed < count($all_links));
?>

Пример #1 Пример што покажува затворачка ознака што го опфаќа последниот нов ред

Array
(
    [0] => test
)

Види Исто така

Белешки од корисници 5 белешки

- Земи резултат од асинхроно прашање
пред 11 години
You can use the following code to execute, for example, 10 simultaneous queries:

$query = "SELECT `field1`, `field2` FROM `table` WHERE `field1`='something'";

$all_links = array();
for($i=0; $i<10; $i++) {
    $link = mysqli_connect("your.mysql.server.here","your@user","pa$$w0rd",DataBase_Name");
    $link->query($query, MYSQLI_ASYNC);
    $all_links[] = $link;
}

$processed = 0;
do {
    $links = $errors = $reject = array();
    foreach ($all_links as $link) {
        $links[] = $errors[] = $reject[] = $link;
    }
    if (!mysqli_poll($links, $errors, $reject, 1)) {
        continue;
    }
    foreach ($links as $link) {
        if ($result = $link->reap_async_query()) {
            print_r($result->fetch_row());
            if (is_object($result))
                mysqli_free_result($result);
        } else die(sprintf("MySQLi Error: %s", mysqli_error($link)));
        $processed++;
    }
} while ($processed < count($all_links));

NOTE: If you get a "Warning: mysqli::query() expects parameter 2 to be long, string given" you have a mysqlnd installation or configuration issue.
l_sanczyk at hotmail dot com
пред 10 години
Sometime it's not clear what does it mean as connection is "ready" . Does it mean the query completed or just some records ready to read?
Is it possible to use asynchronous (unblocking) and unbuffered SELECT-query? I.e. with both MYSQLI_ASYNC|MYSQLI_USE_RESULT

I want some poll-like code inside my loop that has four case-options:
1. Poll wait time-out is over but no records of result set is ready.
2. One or more records are ready to read (but the query still running).
3. Query is successfully over (completed; no more records).
4. Error.

Is it possible?

As I understood the operation of reading a record from unbuffered query is blocking if there are no ready records at the moment, and there is no functions to get out how much records are ready ?
shestero at meta dot ua
3 години пред
It is worth to note that example from this docs will results in waiting for all queries to be executed before you can reap your results.

5 years ago bishop from php.net has posted solution that allow to fetch results of each query immediately after executed. For some reason this has never been merged to php manual.

https://bugs.php.net/bug.php?id=70505

The documentation for mysqli_poll suggests iterating through the links after running the poll:

    foreach ($links as $link) {
        if ($result = $link->reap_async_query()) {
            print_r($result->fetch_row());
            if (is_object($result))
                mysqli_free_result($result);
        } else die(sprintf("MySQLi Error: %s", mysqli_error($link)));
        $processed++;
    }

This is not a good way to go about it when you have queries of different run-times, because mysqli_reap_async_query() blocks until the query resolves, which is contrary to the nature of the async poll.

An approach that respects multiple queries of different run times is to run a reaping function over each of the modified arrays:

    $count = mysqli_poll($read, $error, $reject, 1);
    if (0 < $count) {
        array_walk($read,   'reap');
        array_walk($error,  'reap');
        array_walk($reject, 'reap');
    }

function reap($link) {
    $result = mysqli_reap_async_query($link);
    if (is_object($result)) {
        print_r($result->fetch_assoc()));
        mysqli_free_result($result);
    } else if (false !== $result) {
        print_r($link);
    } else {
        die(mysqli_error($link));
    }
}

This also handles the case where the query is a CRUD and the result of reap_async_query isn't a result object.
marcin dot wanat at gmail dot com
пред 8 години
Improved version of using polling to check for slow query,
"KILL #ID" does not work if id of the myqsli link is the same as the one that kills it,
so you have to disconnect from mysqli and connect again with new link to 
kill the #ID.
<?php
// Slow SQL Query
      $SelectSql = 'SELECT * FROM SLOW_QUERY';
      $link = mysqli_connect('localhost','user','pass','database');
      mysqli_query($SelectSql, MYSQLI_ASYNC);
      $thread_id = mysqli_thread_id($link);
      // Ignore user abort so we can kill the query
      ignore_user_abort(true);
      $MaxTime = 5; // seconds
      $Overtime = false;
      $StartTime = time();
      do
      {
          // Poll MySQL
        $links = $errors = $reject = array($link);
        $poll = mysqli_poll($links, $errors, $reject, 0, 500000);
         // Check if the connection is aborted and the query was killed
        if (connection_aborted()) {
          $link_new = mysqli_connect('localhost','user','pass','database');
          mysqli_kill($link_new, $thread_id);
          $kill = mysqli_kill($link_new, $thread_id);
          if ($kill)
          {
            die();
          }
        }
        $EndTime = time();
        // Check overtime, kill if detected overtime
        if ($EndTime - $StartTime > $MaxTime)
        {
          $link_new = mysqli_connect('localhost','user','pass','database');
          mysqli_kill($link_new, $thread_id);
          $Overtime = true;
          echo 'Error: Query took over '.$Overtime.'.';
        }
      } while (!$poll && $Overtime == false);
?>
marcin dot wanat at gmail dot com
пред 8 години
This is how you can test and terminate slow query with polling.

<?php
// Slow SQL Query
      $SelectSql = 'SELECT * FROM SLOW_QUERY';
      $link = mysqli_connect('localhost','user','pass','database');
      mysqli_query($SelectSql, MYSQLI_ASYNC);
      $thread_id = mysqli_thread_id($link);
      // Ignore user abort so we can kill the query
      ignore_user_abort(true);
      $MaxTime = 5; // seconds
      $Overtime = false;
      $StartTime = time();
      do
      {
          // Poll MySQL
        $links = $errors = $reject = array($link);
        $poll = mysqli_poll($links, $errors, $reject, 0, 500000);
         // Check if the connection is aborted and the query was killed
        if (connection_aborted() && mysqli_kill($link, $thread_id)) {
            die();
        }
        $EndTime = time();
        // Check overtime, kill if detected overtime
        if ($EndTime - $StartTime > $MaxTime)
        {
          mysqli_kill($link, $thread_id);
          $Overtime = true;
          echo 'Error: Query took over '.$Overtime.'.';
        }
      } while (!$poll && $Overtime == false);
?>
На оваа страница

Автоматски outline од активната документација.

Насловите ќе се појават тука по вчитување.

Попрегледно читање

Примерите, changelog табелите и user notes се визуелно издвоени за да не се губат во долгата содржина.

Брз совет Користи го outline-от Скокни директно на главните секции од активната страница.
Извор Оригиналниот линк останува достапен Кога ти треба целосен upstream context, отвори го PHP.net во нов tab.