使用 PHP 连接到云数据库:AWS DynamoDB、Azure Cosmos DB、Google Cloud SQL

admin 阅读:90 2024-05-22

php 可连接至 aws dynamodb、azure cosmos db 和 google cloud sql,方法如下:aws dynamodb:使用 dynamodbclient 类。azure cosmos db:使用 tablerestproxy 类。google cloud sql:使用 pdo 连接。

使用 PHP 连接到云数据库:AWS DynamoDB、Azure Cosmos DB、Google Cloud SQL

利用 PHP 连接至云数据库:AWS DynamoDB、Azure Cosmos DB、Azure Cosmos DB、Google Cloud SQL

AWS DynamoDB

use AwsDynamoDbDynamoDbClient;

$client = new DynamoDbClient([
    'region' => 'us-east-1',
    'credentials' => [
        'key' => 'your-<a style='color:#f60; text-decoration:underline;' href="https://www.codesou.cn/" target="_blank">access</a>-key',
        'secret' => 'your-secret-key',
    ],
]);

Azure Cosmos DB

use MicrosoftAzureStorageTableTableRestProxy;

$accountName = 'your-account-name';
$accountKey = 'your-account-key';
$tableName = 'your-table-name';

$connection = new TableRestProxy(
    $accountName,
    $accountKey,
    'https://accountname.table.usgovcloudapi.net'
);

$cloudTable = $connection->getTable($tableName);

Google Cloud SQL

use PDO;

$username = 'your-username';
$password = 'your-password';
$database = 'your-database';
$host = 'your-host';
$socket = 'your-unix-socket';

try {
    $conn = new PDO(
        "<a style='color:#f60; text-decoration:underline;' href="https://www.codesou.cn/" target="_blank">mysql</a>:dbname=$database;host=$host;unix_socket=$socket",
        $username,
        $password,
        [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
    );
} catch (PDOException $e) {
    echo "Failed connecting to Google Cloud SQL: " . $e->getMessage();
}
声明

1、部分文章来源于网络,仅作为参考。
2、如果网站中图片和文字侵犯了您的版权,请联系1943759704@qq.com处理!