shuchkin/react-mysqli

Asynchronous & non-blocking MySQL driver for React.PHP

0.1.11 2020-12-06 22:03 UTC

This package is auto-updated.

Last update: 2024-11-07 07:24:18 UTC


README

Asynchronous & non-blocking MySQLi driver for ReactPHP.

Require php-mysqlnd extension

Install

composer require shuchkin/react-mysqli

CONNECTION and SELECT

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

$db = \Shuchkin\ReactMySQLi\Client::connect($loop, 'localhost', 'root', '', 'my_db' );

$db->query('SELECT id,name,email FROM user')->then(
    function (\Shuchkin\ReactMySQLi\Result $result) {
        print_r( $result->all() );
    },
    function ( \Exception $ex ) {
        trigger_error( $ex->getMessage() );
    }
);
$loop->run();
Array
(
    [0] => stdClass Object
        (
            [id] => 1
            [name] => Gianni Rodari
            [email] => gianni.rodari@example.com
        )

    [1] => stdClass Object
        (
            [id] => 2
            [name] => Rikki-Tikki-Tavi
            [email] => mangoose@example.com
        )

)

INSERT

$db->query("INSERT INTO user SET name='Sergey',email='sergey.shuchkin@gmail.com'")->then(
    function (\Shuchkin\ReactMySQLi\Result $result) {
        print_r($result->insert_id); // 12345
    },
    function ( \Exception $ex ) {
        trigger_error( $ex->getMessage() );
    }
);

UPDATE

$db->query("UPDATE user SET email='sergey@example.com' WHERE id=12345")->then(
    function (\Shuchkin\ReactMySQLi\Result $result) {
        print_r($result->affected_rows);
    },
    function ( \Exception $ex ) {
        trigger_error( $ex->getMessage() );
    }
);

DELETE

$db->query('DELETE FROM user WHERE id=12345')->then(
    function (\Shuchkin\ReactMySQLi\Result $result) {
        print_r($result->affected_rows);
    },
    function ( \Exception $ex ) {
        trigger_error( $ex->getMessage() );
    }
);