我有一个客户摘要页面,我想在各个位置显示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>
进一步进入正文 - 然后FAILS显示任何数据,除非我删除上面的php部分,如果我删除上面只显示3个recrods之一,除非我删除switch语句(也试过if else如果不是切换同样的问题)
<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>
非常感谢任何解决方案/建设性反馈。提前致谢
答案 0 :(得分:0)
您需要调整结果指针以指向结果集的开头,以便您可以再次迭代它。为此使用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>