atrox/async-mysql

React.PHP 的异步非阻塞 MySQL 驱动

dev-master 2018-02-10 21:05 UTC

This package is not auto-updated.

Last update: 2024-09-20 03:04:25 UTC


README

React.PHP 的异步非阻塞 MySQL 驱动,用于 React.PHP

安装

将此内容添加到您的 composer.json 文件中

{
  "require": {
    "atrox/async-mysql": "dev-master"
  },
  "repositories": [
    {
      "type": "vcs",
      "url": "https://github.com/kaja47/async-mysql"
    }
  ]
}

用法

创建 AsyncMysql 实例并调用 query 方法。它将返回一个 Promise,该 Promise 包含一个 mysqli_result 对象,查询完成后立即解决。

<?php

$loop = React\EventLoop\Factory::create();

$makeConnection = function () {
  return mysqli_connect('localhost', 'user', 'pass', 'dbname');
};

$mysql = new Atrox\AsyncMysql($loop, $makeConnection);
$mysql->query('select * from ponies_and_unicorns')->then(
  function ($result) { writeHttpResponse(json_encode($result->fetch_all(MYSQLI_ASSOC))); $result->close(); },
  function ($error)  { writeHeader500(); }
);