MysqlndUhConnection::reapQuery

(PECL mysqlnd-uh >= 1.0.0-alpha)

MysqlndUhConnection::reapQueryRécupère le résultat depuis une requête asynchrone

Description

public bool MysqlndUhConnection::reapQuery ( mysqlnd_connection $connection )

Récupère le résultat depuis une requête asynchrone.

Liste de paramètres

connection

Gestionnaire de connexion Mysqlnd. Ne pas modifier !

Valeurs de retour

Retourne TRUE en cas de succès. Sinon, retourne FALSE

Exemples

Exemple #1 Exemple avec MysqlndUhConnection::reapQuery()

  1. <?php
  2. class proxy extends MysqlndUhConnection {
  3. public function reapQuery($res) {
  4. printf("%s(%s)\n", __METHOD__, var_export(func_get_args(), true));
  5. $ret = parent::reapQuery($res);
  6. printf("%s retourne %s\n", __METHOD__, var_export($ret, true));
  7. return $ret;
  8. }
  9. }
  10. mysqlnd_uh_set_connection_proxy(new proxy());
  11.  
  12. $conn1 = new mysqli("localhost", "root", "", "test");
  13. $conn2 = new mysqli("localhost", "root", "", "test");
  14.  
  15. $conn1->query("SELECT 1 as 'one', SLEEP(1) AS _sleep FROM DUAL", MYSQLI_ASYNC | MYSQLI_USE_RESULT);
  16. $conn2->query("SELECT 1.1 as 'one dot one' FROM DUAL", MYSQLI_ASYNC | MYSQLI_USE_RESULT);
  17.  
  18. $links = array(
  19. $conn1->thread_id => array('link' => $conn1, 'processed' => false),
  20. $conn2->thread_id => array('link' => $conn2, 'processed' => false)
  21. );
  22.  
  23. $saved_errors = array();
  24. do {
  25. $poll_links = $poll_errors = $poll_reject = array();
  26. foreach ($links as $thread_id => $link) {
  27. if (!$link['processed']) {
  28. $poll_links[] = $link['link'];
  29. $poll_errors[] = $link['link'];
  30. $poll_reject[] = $link['link'];
  31. }
  32. }
  33. if (0 == count($poll_links))
  34. break;
  35.  
  36. if (0 == ($num_ready = mysqli_poll($poll_links, $poll_errors, $poll_reject, 0, 200000)))
  37. continue;
  38.  
  39. if (!empty($poll_errors)) {
  40. die(var_dump($poll_errors));
  41. }
  42.  
  43. foreach ($poll_links as $link) {
  44. $thread_id = mysqli_thread_id($link);
  45. $links[$thread_id]['processed'] = true;
  46.  
  47. if (is_object($res = mysqli_reap_async_query($link))) {
  48. // result set object
  49. while ($row = mysqli_fetch_assoc($res)) {
  50. // eat up all results
  51. var_dump($row);
  52. }
  53. mysqli_free_result($res);
  54. } else {
  55. // either there is no result (no SELECT) or there is an error
  56. if (mysqli_errno($link) > 0) {
  57. $saved_errors[$thread_id] = mysqli_errno($link);
  58. printf("'%s' caused %d\n", $links[$thread_id]['query'], mysqli_errno($link));
  59. }
  60. }
  61. }
  62. } while (true);
  63. ?>

L'exemple ci-dessus va afficher :

proxy::reapQuery(array (
  0 => NULL,
))
proxy::reapQuery retourne true
array(1) {
  ["one dot one"]=>
  string(3) "1.1"
}
proxy::reapQuery(array (
  0 => NULL,
))
proxy::reapQuery retourne true
array(2) {
  ["one"]=>
  string(1) "1"
  ["_sleep"]=>
  string(1) "0"
}

Voir aussi

LoadingChargement en cours