【发布时间】: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 " . $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