将 PHP 与 Azure Database for MySQL 灵活服务器配合使用

本快速入门演示如何使用 PHP 应用程序连接到 Azure Database for MySQL 灵活服务器。 同时还介绍了如何使用 SQL 语句在数据库中查询、插入、更新和删除数据。 本文假设你熟悉使用 PHP 进行开发,并且不熟悉如何使用 Azure Database for MySQL 灵活服务器。

先决条件

此快速入门使用以下任意指南中创建的资源作为起点:

准备客户端工作站

  1. 如果使用 专用访问(虚拟网络集成)创建了灵活服务器,则需要从服务器所在的同一虚拟网络中的资源连接到服务器。 可以创建虚拟机并将其添加到使用灵活服务器创建的虚拟网络中。 请参阅使用 Azure CLI 为 Azure Database for MySQL 灵活服务器创建和管理虚拟网络

  2. 如果使用“公共访问(允许的 IP 地址)”创建了灵活服务器,可以将本地 IP 地址添加到服务器上的防火墙规则列表中。 请参阅使用 Azure CLI 为 Azure Database for MySQL 灵活服务器管理防火墙规则

安装 PHP

在自己的服务器上安装 PHP,或者创建包括 PHP 的 Azure Web 应用。 有关详细信息,请参阅 创建和管理防火墙规则

  1. 下载 PHP 7.1.4 非线程安全 (x64) 版本
  2. 安装 PHP 并参阅 PHP 手册了解进一步的配置。

获取连接信息

获取连接到 Azure Database for MySQL 灵活服务器实例所需的连接信息。 需要完全限定的服务器名称和登录凭据。

  1. 登录 Azure 门户

  2. 在 Azure 门户的左侧菜单中,选择“ 所有资源”,然后搜索已创建的服务器(在以下示例中,替换为 <server> 有效值)。

  3. 选择服务器名称。

  4. 从服务器的“概览”面板中记下“服务器名称”和“服务器管理员登录名”。 如果忘记了密码,也可通过此面板来重置密码。

使用 PHP 中的 TLS/SSL 连接到灵活服务器

若要从应用程序通过 TLS/SSL 与灵活服务器建立加密连接,请参阅以下代码示例。 可以从中下载通过 TLS/SSL https://dl.cacerts.digicert.com/DigiCertGlobalRootCA.crt.pem进行通信所需的证书。

$host$username$password$db_name 参数替换为你自己的值。

<?php
$conn = mysqli_init();
mysqli_ssl_set(
    $conn,
    null,
    null,
    "/var/www/html/DigiCertGlobalRootCA.crt.pem",
    null,
    null
);
mysqli_real_connect(
    $conn,
    "<server>.mysql.database.chinacloudapi.cn",
    "<username>",
    "<password>",
    "<database>",
    3306,
    MYSQLI_CLIENT_SSL
);
if (mysqli_connect_errno($conn)) {
    die("Failed to connect to MySQL: " . mysqli_connect_error());
}
?>

进行连接并创建表

使用以下代码通过 SQL 语句连接和创建表 CREATE TABLE

代码使用 PHP 中包括的 MySQL 改进的扩展 (mysqli) 类。 代码调用 mysqli_initmysqli_real_connect 方法连接到 MySQL。 然后,代码调用 mysqli_query 方法来运行查询。 然后,代码调用 mysqli_close 方法来关闭连接。

还可以使用 mysqli 扩展提供的面向对象的接口连接到 Azure Database for MySQL。

$host$username$password$db_name 参数替换为你自己的值。

<?php
$host = "<server>.mysql.database.chinacloudapi.cn";
$username = "<username>";
$password = "<password>";
$db_name = "<database>";

//Establishes the connection
$conn = mysqli_init();
mysqli_real_connect($conn, $host, $username, $password, $db_name, 3306);
if (mysqli_connect_errno($conn)) {
    die("Failed to connect to MySQL: " . mysqli_connect_error());
}

// Run the create table query
if (
    mysqli_query(
        $conn,
        '
CREATE TABLE Products (
`Id` INT NOT NULL AUTO_INCREMENT ,
`ProductName` VARCHAR(200) NOT NULL ,
`Color` VARCHAR(50) NOT NULL ,
`Price` DOUBLE NOT NULL ,
PRIMARY KEY (`Id`)
);
'
    )
) {
    printf("Table created\n");
}

//Close the connection
mysqli_close($conn);
?>

插入数据

使用以下代码通过 SQL 语句连接和插入数据 INSERT

代码使用 PHP 中包括的 MySQL 改进的扩展 (mysqli) 类。 代码使用 mysqli_prepare 方法来创建已准备的 insert 语句,然后使用 mysqli_stmt_bind_param 方法绑定每个已插入列值的参数。 代码使用 mysqli_stmt_execute 方法运行语句,然后使用 mysqli_stmt_close 方法关闭语句。

还可以使用 mysqli 扩展提供的面向对象的接口连接到 Azure Database for MySQL。

$host$username$password$db_name 参数替换为你自己的值。

<?php
$host = "<server>.mysql.database.chinacloudapi.cn";
$username = "<username>";
$password = "<password>";
$db_name = "<database>";

//Establishes the connection
$conn = mysqli_init();
mysqli_real_connect($conn, $host, $username, $password, $db_name, 3306);
if (mysqli_connect_errno($conn)) {
    die("Failed to connect to MySQL: " . mysqli_connect_error());
}

//Create an Insert prepared statement and run it
$product_name = "BrandNewProduct";
$product_color = "Blue";
$product_price = 15.5;
if (
    $stmt = mysqli_prepare(
        $conn,
        "INSERT INTO Products (ProductName, Color, Price) VALUES (?, ?, ?)"
    )
) {
    mysqli_stmt_bind_param(
        $stmt,
        "ssd",
        $product_name,
        $product_color,
        $product_price
    );
    mysqli_stmt_execute($stmt);
    printf("Insert: Affected %d rows\n", mysqli_stmt_affected_rows($stmt));
    mysqli_stmt_close($stmt);
}

// Close the connection
mysqli_close($conn);
?>

读取数据

使用以下代码进行连接,并使用 SELECT SQL 语句读取数据。 代码使用 PHP 中包括的 MySQL 改进的扩展 (mysqli) 类。 代码使用 mysqli_query 方法执行 SQL 查询,并使用 mysqli_fetch_assoc 方法提取生成的行。

还可以使用 mysqli 扩展提供的面向对象的接口连接到 Azure Database for MySQL。

$host$username$password$db_name 参数替换为你自己的值。

<?php
$host = "<server>.mysql.database.chinacloudapi.cn";
$username = "<username>";
$password = "<password>";
$db_name = "<database>";

//Establishes the connection
$conn = mysqli_init();
mysqli_real_connect($conn, $host, $username, $password, $db_name, 3306);
if (mysqli_connect_errno($conn)) {
    die("Failed to connect to MySQL: " . mysqli_connect_error());
}

//Run the Select query
printf("Reading data from table: \n");
$res = mysqli_query($conn, "SELECT * FROM Products");
while ($row = mysqli_fetch_assoc($res)) {
    var_dump($row);
}

//Close the connection
mysqli_close($conn);
?>

更新数据

使用以下代码通过 SQL 语句连接和更新数据 UPDATE

代码使用 PHP 中包括的 MySQL 改进的扩展 (mysqli) 类。 代码使用 mysqli_prepare 方法来创建已准备的 update 语句,然后使用 mysqli_stmt_bind_param 方法绑定每个已更新列值的参数。 代码使用 mysqli_stmt_execute 方法运行语句,然后使用 mysqli_stmt_close 方法关闭语句。

还可以使用 mysqli 扩展提供的面向对象的接口连接到 Azure Database for MySQL。

$host$username$password$db_name 参数替换为你自己的值。

<?php
$host = "<server>.mysql.database.chinacloudapi.cn";
$username = "<username>";
$password = "<password>";
$db_name = "<database>";

//Establishes the connection
$conn = mysqli_init();
mysqli_real_connect($conn, $host, $username, $password, $db_name, 3306);
if (mysqli_connect_errno($conn)) {
    die("Failed to connect to MySQL: " . mysqli_connect_error());
}

//Run the Update statement
$product_name = "BrandNewProduct";
$new_product_price = 15.1;
if (
    $stmt = mysqli_prepare(
        $conn,
        "UPDATE Products SET Price = ? WHERE ProductName = ?"
    )
) {
    mysqli_stmt_bind_param($stmt, "ds", $new_product_price, $product_name);
    mysqli_stmt_execute($stmt);
    printf("Update: Affected %d rows\n", mysqli_stmt_affected_rows($stmt));

    //Close the connection
    mysqli_stmt_close($stmt);
}

mysqli_close($conn);

?>

删除数据

使用以下代码进行连接,并使用 DELETE SQL 语句读取数据。

代码使用 PHP 中包括的 MySQL 改进的扩展 (mysqli) 类。 代码使用 mysqli_prepare 方法来创建已准备的 delete 语句,然后使用 mysqli_stmt_bind_param 方法绑定语句中的 where 子句的参数。 代码使用 mysqli_stmt_execute 方法运行语句,然后使用 mysqli_stmt_close 方法关闭语句。

还可以使用 mysqli 扩展提供的面向对象的接口连接到 Azure Database for MySQL。

$host$username$password$db_name 参数替换为你自己的值。

<?php
$host = "<server>.mysql.database.chinacloudapi.cn";
$username = "<username>";
$password = "<password>";
$db_name = "<database>";

//Establishes the connection
$conn = mysqli_init();
mysqli_real_connect($conn, $host, $username, $password, $db_name, 3306);
if (mysqli_connect_errno($conn)) {
    die("Failed to connect to MySQL: " . mysqli_connect_error());
}

//Run the Delete statement
$product_name = "BrandNewProduct";
if (
    $stmt = mysqli_prepare($conn, "DELETE FROM Products WHERE ProductName = ?")
) {
    mysqli_stmt_bind_param($stmt, "s", $product_name);
    mysqli_stmt_execute($stmt);
    printf("Delete: Affected %d rows\n", mysqli_stmt_affected_rows($stmt));
    mysqli_stmt_close($stmt);
}

//Close the connection
mysqli_close($conn);
?>