PHP code example of nstwf / mysql-connection

1. Go to this page and download the library: Download nstwf/mysql-connection library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

nstwf / mysql-connection example snippets


$factory = new \Nstwf\MysqlConnection\Factory\ConnectionFactory(new \React\MySQL\Factory());
$connection = $factory->createConnection('localhost:3306');

$connection
    ->transaction(function (\Nstwf\MysqlConnection\ConnectionInterface $connection) {
        return $connection->query('update users set name = "Tim" where id = 3');
    })
    ->then(function () {
        echo 'OK';
    }, function (\Throwable $throwable) {
        echo $throwable->getMessage();
    });

$connection->quit();

$factory = new \Nstwf\MysqlConnection\Factory\ConnectionFactory(new \React\MySQL\Factory());
$connection = $factory->createConnection('localhost:3306');

$connection
    ->transaction(function (\Nstwf\MysqlConnection\ConnectionInterface $connection) {
        return $connection->query('update users set name = "Tim" where id = 3');
    })
    ->then(function () {
        echo 'OK';
    }, function (\Throwable $throwable) {
        echo $throwable->getMessage();
    });

$connection
    ->query("BEGIN")
    ->then(
        fn() => $connection->query("COMMIT"),
        function (\Throwable $throwable) use ($connection)  {
        return $connection->query("ROLLBACK")
                          ->then(fn() => $throwable);
        }
    );

$connection
    ->begin()
    ->then(function () {
        echo 'Begin';
    }, function (\Throwable $throwable) {
        echo $throwable->getMessage();
    });

$connection->query("BEGIN");
// or
$connection->query("START TRANSACTION");

$connection
    ->commit()
    ->then(function () use ($connection) {
        echo 'Commit';
    }, function (\Throwable $throwable) {
        echo $throwable->getMessage();
    });

$connection->query("COMMIT");

$connection
    ->rollback()
    ->then(function () use ($connection) {
        echo 'Rollback';
    }, function (\Throwable $throwable) {
        echo $throwable->getMessage();
    });

$connection->query("ROLLBACK");