Home  >  Article  >  Backend Development  >  Connect to cloud databases using PHP: AWS DynamoDB, Azure Cosmos DB, Google Cloud SQL

Connect to cloud databases using PHP: AWS DynamoDB, Azure Cosmos DB, Google Cloud SQL

PHPz
PHPzOriginal
2024-06-06 11:21:57593browse

PHP can connect to AWS DynamoDB, Azure Cosmos DB, and Google Cloud SQL as follows: AWS DynamoDB: Using the DynamoDbClient class. Azure Cosmos DB: Use the TableRestProxy class. Google Cloud SQL: Connect using PDO.

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

Use PHP to connect to cloud databases: 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();
}

The above is the detailed content of Connect to cloud databases using PHP: AWS DynamoDB, Azure Cosmos DB, Google Cloud SQL. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn