Swoole 快速起步:協程 - MySQL

 demo.php

<?php
go(function () {

    $swoole_mysql = new Swoole\Coroutine\MySQL();
    // 連接
    $swoole_mysql->connect([
        'host' => '127.0.0.1',
        'port' => 3306,
        'user' => 'root',
        'password' => 'root',
        'database' => 'test',
        // 使用fetch/fetchAll獲取結果集必須開啓(v4.0 以上)
        'fetch_mode' => true
    ]);

    // 預處理
    $stmt = $swoole_mysql->prepare("SELECT * FROM test WHERE id < ?");
    // 執行
    $rs = $stmt->execute([3]);
    // 遍歷結果集
    while($ret = $stmt->fetch())
    {
        var_dump($ret);
    }

    // 開啓事務
    $swoole_mysql->begin();

    // 直接執行 sql
    $swoole_mysql->query('INSERT INTO test SET content = "insert something .."');
    $swoole_mysql->query('UPDATE test SET content = "update something .." WHERE id = 1');

    // 提交事務
    $swoole_mysql->commit();

    // 捕獲異常&&事務回滾
    // $swoole_mysql->rollback();

});

echo date('Y-m-d H:i:s') . ":go ..\n";

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章