PHP code example of cloudtay / ripple-database

1. Go to this page and download the library: Download cloudtay/ripple-database 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/ */

    

cloudtay / ripple-database example snippets


 declare(strict_types=1);

 = "mysql:host=127.0.0.1;port=3306;dbname=test_db;charset=utf8mb4";
$pdo = new PDO($dsn, 'root', '123456');

// 创建表
$pdo->exec("CREATE TABLE IF NOT EXISTS users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    email VARCHAR(255) UNIQUE
)");

// 预处理语句
$stmt = $pdo->prepare("INSERT INTO users (name, email) VALUES (?, ?)");
$stmt->execute(['Alice', '[email protected]']);

// 查询数据
$result = $pdo->query("SELECT * FROM users WHERE name = 'Alice'")->fetch();
echo "User: {$result['name']} - {$result['email']}\n";

$pool = new PDOPool($dsn, $user, $password, [
    // PDO选项
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,

    // 连接池选项
    'pool_max' => 20,                    // 最大连接数,自动扩容
    'pool_min' => 5,                     // 最小连接数,初始化时自动连接
    'pool_connection_timeout' => 300,   // 连接超时时间(惰性检查)
]);

// 测试覆盖的数据类型
$testData = [
    'tiny_col' => 1,                    // TINYINT
    'small_col' => 32000,               // SMALLINT
    'int_col' => 2147483647,            // INT
    'big_col' => 9223372036854775807,   // BIGINT
    'float_col' => 3.14159,             // FLOAT
    'double_col' => 2.718281828459,     // DOUBLE
    'decimal_col' => '12345.67',        // DECIMAL
    'char_col' => 'char_text',          // CHAR
    'varchar_col' => 'varchar_text',    // VARCHAR
    'text_col' => 'this is text',       // TEXT
    'blob_col' => random_bytes(16),     // BLOB
    'json_col' => json_encode(['a' => 1, 'b' => 2]), // JSON
    'date_col' => '2025-09-20',         // DATE
    'datetime_col' => '2025-09-20 19:00:00', // DATETIME
    'timestamp_col' => '2025-09-20 19:00:00', // TIMESTAMP
    'time_col' => '12:34:56',           // TIME
    'year_col' => 2025,                 // YEAR
    'bool_col' => 1,                    // BOOLEAN
    'enum_col' => 'b',                  // ENUM
    'set_col' => 'x,y',                 // SET
    'emoji_col' => '😀🚀✨'            // UTF-8 Emoji
];

 declare(strict_types=1);

se Ripple\Sync\Channel;
use Ripple\Sync\WaitGroup;

use function Co\go;

$dsn = "mysql:host=127.0.0.1;port=3306;dbname=test_db;charset=utf8mb4";
$pdo = new PDOPool($dsn, 'root', '123456', [
    'pool_max' => 10,
    'pool_min' => 2
]);

$channel = new Channel(10);
for ($i = 0; $i < 10; $i++) {
    go(static function () use ($pdo, $channel) {
        $result = $pdo->query("SELECT SLEEP(1);");
        $channel->send($result);
    });
}

while ($result = $channel->receive()) {
    echo json_encode($result->fetch(), JSON_UNESCAPED_UNICODE) . PHP_EOL;
}



use Ripple\Database\PDO;

$pdo = new PDO($dsn, 'root', '123456');

try {
    $pdo->beginTransaction();

    $stmt1 = $pdo->prepare("UPDATE accounts SET balance = balance - ? WHERE name = ?");
    $stmt1->execute([100.00, 'Alice']);

    $stmt2 = $pdo->prepare("UPDATE accounts SET balance = balance + ? WHERE name = ?");
    $stmt2->execute([100.00, 'Bob']);

    $pdo->commit();
    echo "Transaction completed successfully\n";

} catch (Exception $e) {
    $pdo->rollback();
    echo "Transaction failed: " . $e->getMessage() . "\n";
}