登录
首页 >  文章 >  php教程

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

时间:2024-05-21 16:51:17 210浏览 收藏

“纵有疾风来,人生不言弃”,这句话送给正在学习文章的朋友们,也希望在阅读本文《使用 PHP 连接到云数据库:AWS DynamoDB、Azure Cosmos DB、Google Cloud SQL》后,能够真的帮助到大家。我也会在后续的文章中,陆续更新文章相关的技术文章,有好的建议欢迎大家在评论留言,非常感谢!

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 Aws\DynamoDb\DynamoDbClient;

$client = new DynamoDbClient([
    'region' => 'us-east-1',
    'credentials' => [
        'key' => 'your-access-key',
        'secret' => 'your-secret-key',
    ],
]);

Azure Cosmos DB

use MicrosoftAzure\Storage\Table\TableRestProxy;

$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(
        "mysql: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();
}

好了,本文到此结束,带大家了解了《使用 PHP 连接到云数据库:AWS DynamoDB、Azure Cosmos DB、Google Cloud SQL》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多文章知识!

相关阅读
更多>
最新阅读
更多>
课程推荐
更多>