如何在 PHP 中打印多个值列

How to print multiple value column in PHP

我有一个关于 Customer 信息的 SQL 服务器数据库,并且 CustomerPhone 有一个多值列,即 C_Phone

这是我的代码:

if (!empty($_GET['customer_id'])){
    // 'id' input from the user
    $id = $_GET['customer_id'];
    // Customer Information
    $sql = "SELECT Customer.C_Code, C_FirstName, C_LastName, C_Email, C_HomeAddress, C_OfficeAddress, C_Phone
            FROM Customer, CustomerPhone
            WHERE Customer.C_code = CustomerPhone.C_code and Customer.C_code = $id;";
    $query = sqlsrv_query($conn, $sql);
    $row = sqlsrv_fetch_array($query);
    if (!empty($row)) {
        echo "
        <div class=\"card\" style=\"width: 25rem; margin-left: auto; margin-right: auto;\">
            <div class=\"card-body\">
                <h5 class=\"card-title\">Customer Information</h5>";
        echo "<p class=\"card-text\">C_code: " . $row['C_Code'] . "</p>";
        echo "<p class=\"card-text\">C_Name: " . $row['C_FirstName'] . " " . $row['C_LastName'] . "</p>";
        echo "<p class=\"card-text\">C_Email: " . $row['C_Email'] . "</p>";
        echo "<p class=\"card-text\">C_HomeAddress: " . $row['C_HomeAddress'] . "</p>";
        echo "<p class=\"card-text\">C_OfficeAddress: " . $row['C_OfficeAddress'] . "</p>";
        echo "<p class=\"card-text\">C_Phone: ";
        while($row = sqlsrv_fetch_array($query))
        {   
            echo $row['C_Phone'] . "<br>";
        }
        echo "</p>";
        echo "
            </div>
        </div>";
    }

这是我得到的结果。如何在第一个数字下打印第二个 phone 数字?

如果我在 SSMS 上 运行 上面的 SQL 查询,我得到的结果是 Customer.C_Code = 7:

至少有两条路线可供选择。您必须根据您的数据库连接功能调整这些想法,因为我没有使用 Microsoft 数据库的经验

嵌套循环

在客户信息的每次迭代中,使用从 Customer 获得的 C_code 对 CustomerPhone 进行第二次查询。如评论中所述,使用 while 语句遍历结果。

while($row=sqlsrv_fetch_array($query) {

必须注意防止内部查询破坏外部查询!

SQL 聚合函数

这涉及进行更复杂的查询。您可以 group by 一个字段并让数据库连接字段:


SELECT 
     Customer.C_Code, C_FirstName, C_LastName, 
     C_Email, C_HomeAddress, C_OfficeAddress, 
     string_agg(C_Phone, ', ') AS phone
FROM Customer
INNER JOIN CustomerPhone ON Customer.C_code = CustomerPhone.C_code
WHERE
     Customer.C_code = ?
GROUP BY Customer.C_code

(注意有些数据库要求每个字段都在 group by 子句中)

(注 2 - 这是一个参数化查询,与准备命令一起使用。请参阅准备语句的文档。这是在查询中使用值的正确方法)

如图所示,这会将它们列为 csv。您也可以使用 <br> 代替逗号来分隔它们。

这种意外行为的原因是您需要从第一个获取的行中回显客户 phone(echo "<p class=\"card-text\">C_Phone: ". $row['C_Phone'] . "<br>"; 而不是 echo "<p class=\"card-text\">C_Phone: ";)。

您还可以考虑以下内容:

  • 使用明确的 JOIN 语法。
  • 使用参数化查询。正如在 documentation 中提到的,sqlsrv_query 函数非常适合一次性查询,应该是执行查询的默认选择,除非有特殊情况,并且 sqlsrv_query函数既做语句准备又做语句执行,可用于执行参数化查询.

以下示例基于您的代码,是您问题的可能解决方案:

if (!empty($_GET['customer_id'])) { 
    // Customer Information
    $sql = "
        SELECT c.C_Code, c.C_FirstName, c.C_LastName, c.C_Email, c.C_HomeAddress, c.C_OfficeAddress, p.C_Phone
        FROM Customer c
        JOIN CustomerPhone p ON c.C_code = p.C_code
        WHERE c.C_code = ?;
    ";
    $params = array($_GET['customer_id']);
    $query = sqlsrv_query($conn, $sql, $params);
    if ($query === false) {
        echo "Error (sqlsrv_query): ".print_r(sqlsrv_errors(), true);
        exit;
    }   
    // fetch data
    $row = sqlsrv_fetch_array($query);
    if (!empty($row)) {
        echo "
        <div class=\"card\" style=\"width: 25rem; margin-left: auto; margin-right: auto;\">
            <div class=\"card-body\">
                <h5 class=\"card-title\">Customer Information</h5>";
        echo "<p class=\"card-text\">C_code: " . $row['C_Code'] . "</p>";
        echo "<p class=\"card-text\">C_Name: " . $row['C_FirstName'] . " " . $row['C_LastName'] . "</p>";
        echo "<p class=\"card-text\">C_Email: " . $row['C_Email'] . "</p>";
        echo "<p class=\"card-text\">C_HomeAddress: " . $row['C_HomeAddress'] . "</p>";
        echo "<p class=\"card-text\">C_OfficeAddress: " . $row['C_OfficeAddress'] . "</p>";
        echo "<p class=\"card-text\">C_Phone: ". $row['C_Phone'] . "<br>";
        while ($row = sqlsrv_fetch_array($query))
        {   
            echo $row['C_Phone'] . "<br>";
        }
        echo "</p>";
        echo "
            </div>
        </div>";
    }
}