PHP (MySQLi) - 在多个位置显示数据

PHP (MySQLi) - Displaying Data in multiple locations

我有一个客户摘要页面,我想在其中显示来自不同位置的 SQL 查询的数据,当我尝试在页面的多个部分显示数据时,该查询目前失败了?

关于原因有什么建议吗?

头部区域:

<?php
// Database settings - data replaced with ######
$servername = "######";
$username = "#######";
$password = "#######";
$dbname = "#######";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
        // Build query and place into variable names SQL
        $sql = "
        SELECT * FROM cust_details
        INNER JOIN cust_details_contacts
        ON
        cust_details.cust_details_id = cust_details_contacts.cust_details_id
        WHERE
        cust_details.cust_details_id=73
        AND
        cust_details_contacts.cust_details_id = 73";
        // Open connection run query and place outcome in variable result
        $result = $conn->query($sql);
        ?>

正文中-作品及展示如下:

<section class="content-header">
<h1>
<?php
$row = mysqli_fetch_array($result,MYSQL_ASSOC);
echo $row['cust_details_name']; ?>
</h1>
<?php include("global_breadcrumb.php"); ?>
</section>

在主体的另一个区域-以下作品和展示:

<div class="box-body">
<?php
$row = mysqli_fetch_array($result,MYSQL_ASSOC);
echo "<h1>" . $row['cust_details_legal'] . "</h1>";
echo "<p>Company Number&nbsp;" . $row['cust_details_company_no'] . "</p>";
?>
///more html content here...
</div>

进一步进入正文 - 这将无法显示任何数据,除非我删除上面的 php 部分,如果我删除上面的内容,则只会显示 3 个记录之一,除非我删除switch 语句(也试过 if else if 而不是 switch 有同样的问题)

<table class="table no-margin">
<thead>
<tr>
<th>Forename</th>
<th>Surname</th>
<th>Phone</th>
<th>Email</th>
<th>Type</th>
</tr>
</thead>

<tbody>

<?php
while ($row = mysqli_fetch_array($result,MYSQL_ASSOC))
{
echo "<tr>";
echo "<td>" . $row['cust_details_contacts_forename'] . "</td>";
echo "<td>" . $row['cust_details_contacts_surname'] . "</td>";
echo "<td>" . $row['cust_details_contacts_phone'] . "</td>";
echo "<td>" . $rpw['cust_details_contacts_email'] . "</td>";

//Also tried if ifelse statement neither worked

switch ($row['cust_details_contacts_type']) {
case "Key";
"<td><span class='label label-info'>". $row['cust_details_contacts_type'] . "</span></td>";
break;
case "Meters";
"<td><span class='label label-warning'>". $row['cust_details_contacts_type'] . "</span></td>";
break;
case "Accounts";
"<td><span class='label label-danger'>". $row['cust_details_contacts_type'] . "</span></td>";
break;  }                   
echo "</tr>";
}
?>
</tbody>
</table>

非常感谢任何解决方案/建设性反馈。提前致谢

您需要将结果指针调整为指向结果集的开头,以便您可以再次遍历它。为此使用 mysqli_data_seek() 函数。

引用如下:

所以你的代码应该是这样的:

<table class="table no-margin">
<thead>
<tr>
<th>Forename</th>
<th>Surname</th>
<th>Phone</th>
<th>Email</th>
<th>Type</th>
</tr>
</thead>

<tbody>

<?php

    mysqli_data_seek($result, 0); // adjust the result pointer to point to the beginning of the result set

    while ($row = mysqli_fetch_array($result,MYSQL_ASSOC)){
        echo "<tr>";
        echo "<td>" . $row['cust_details_contacts_forename'] . "</td>";
        echo "<td>" . $row['cust_details_contacts_surname'] . "</td>";
        echo "<td>" . $row['cust_details_contacts_phone'] . "</td>";
        echo "<td>" . $rpw['cust_details_contacts_email'] . "</td>";

        switch ($row['cust_details_contacts_type']) {
            case "Key":
                echo "<td><span class='label label-info'>". $row['cust_details_contacts_type'] . "</span></td>";
                break;
            case "Meters":
                echo "<td><span class='label label-warning'>". $row['cust_details_contacts_type'] . "</span></td>";
                break;
            case "Accounts":
                echo "<td><span class='label label-danger'>". $row['cust_details_contacts_type'] . "</span></td>";
                break;  
        }                   
        echo "</tr>";
    }
?>
</tbody>
</table>