【问题标题】:PHP (MySQLi) - Displaying Data in multiple locationsPHP (MySQLi) - 在多个位置显示数据
【发布时间】:2026-02-11 17:25:03
【问题描述】:

我有一个客户摘要页面,我想在该页面上显示来自不同位置的 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 部分,如果我删除上述部分,除非我删除 switch 语句,否则仅显示 3 个记录之一(如果其他情况也尝试过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>

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

【问题讨论】:

  • 在最后一节中,在最后一个回显中,您设置的是 $rpw 而不是 $row,这可能无法解决您的问题,但这是一个错误
  • 菲特。我检查了原始来源,当我将它发布到源中时,它是一个错误。不过感谢您的反馈。

标签: php mysqli while-loop switch-statement echo


【解决方案1】:

您需要调整结果指针以指向结果集的开头,以便您可以再次遍历它。为此使用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>

【讨论】:

  • Rajdeep,非常感谢它(部分)工作!现在显示三行,但 SWITCH 语句不显示。有什么想法吗?另外,我应该每次在文档中回显数据时使用 mysqli_data_seek 还是仅在使用 while 循环时使用?
  • @Nathwales 根据文档,您可以使用 mysqli_data_seek() 函数 anywhere (不一定在循环之前),您认为需要将结果指针调整为任意结果集中的行。
  • @Nathwales 对于 switch-case 语句,您没有 echo 字符串,您只是将它们括在双引号中。做echo "&lt;td&gt;&lt;span ...&lt;/td&gt;";
  • 感谢您的反馈。我不明白你对 switch case 语句的最后评论,你说 _“你没有回显字符串,你只是用双引号将它们括起来。做 _echo "&lt;td&gt;&lt;span ...&lt;/td&gt;";”。我的说法是:echo "&lt;td&gt;&lt;span class='label label-warning'&gt;". $row['cust_details_contacts_type'] . "&lt;/span&gt;&lt;/td&gt;"; 这是不正确的吗?如果是这样,您会按原样评论代码吗?再次感谢。
  • @Nathwales 您的陈述看起来不错,但case 陈述中有一个拼写错误,我之前没有注意到。 case "Key"; 应该是 case "Key":case "Meters"; 应该是 case "Meters": 等等。我已经更新了我的答案。还要确保案例标签正确,即case "Key":case "key": 不同。