Cette rubrique explique comment utiliser PHP pour se connecter à un cluster AnalyticDB for MySQL.
Prérequis
Sous Linux, installez php-mysql 5,1.x.
Sous Windows, installez php_MySQL.dll.
Pour vous connecter à un cluster AnalyticDB for MySQL via Internet, ajoutez l'adresse IP de votre appareil à la liste d'autorisation du cluster AnalyticDB for MySQL. Pour plus d'informations, consultez la section Configurer une liste d'autorisation.
Utiliser MySQLi pour se connecter à un cluster AnalyticDB for MySQL
//The endpoint of the cluster. You can obtain the endpoint on the Cluster Information page in the AnalyticDB for MySQL console.
$ads_server_name="am-bp***.ads.aliyuncs.com";
//The account used to connect to the cluster. offers the following types of accounts: privileged accounts and standard accounts.
$ads_username="account_name";
//The password of the account used to connect to the cluster.
$ads_password="account_password";
//The name of the database in the cluster.
$ads_database="db_name";
//The port used to connect to the cluster.
$ads_port=3306;
//Connect to the cluster.
$ads_conn=mysqli_connect($ads_server_name,$ads_username,$ads_password,$ads_database, $ads_port);
$strsql="SELECT user_id FROM my_ads_db.my_first_table limit 20;";
$result=mysqli_query($ads_conn, $strsql);
while($row = mysqli_fetch_array($result)) {
//Obtain data in the user_id column.
echo $row["user_id"];
}
Utiliser PDO pour se connecter à un cluster AnalyticDB for MySQL
Remarque
Pour activer PrepareStatement avec
lors de la connexion à un cluster AnalyticDB for MySQL, consultez la section « PDO » de la rubrique
Activer PrepareStatement pour un client dans différents langages de programmation
.
//The endpoint of the cluster. You can obtain the endpoint on the Cluster Information page in the AnalyticDB for MySQL console.
$ads_server_name = "am-bp***.ads.aliyuncs.com";
//The account used to connect to the cluster. offers the following types of accounts: privileged accounts and standard accounts.
$ads_username = "account_name";
//The password of the account used to connect to the cluster.
$ads_password = "account_password";
//The name of the database in the cluster.
$ads_database = 'db_name';
//The port used to connect to the cluster.
$ads_port = 3306;
$dsn = "mysql:host={$ads_server_name};dbname={$ads_database};port={$ads_port}";
try {
$dbh = new PDO($dsn, $ads_username, $ads_password);
echo 'PDO Success !';
} catch (PDOException $e) {
echo 'PDO Connection failed: ' . $e->getCode() ."\n" . $e->getMessage() ."\n". $e->getTraceAsString();
}