【发布时间】:2014-09-06 19:07:33
【问题描述】:
这是我在 SO 中的第一篇文章,如果我打开一个现有问题,我深表歉意。因为我在 Google 中找不到结果。很抱歉,但我对 PHP PDO 和学习阶段还很陌生。
回到我的问题,目前我正在构建我妻子的客户访问日志,但我对结果感到困惑。我有两张表,一张存储客户信息,另一张存储访问详细信息。我在这里上传了测试表:SQL Fiddle
下面是我当前的编码,我正在使用 PHP PDO 而
<?php
require_once 'dbconnect.php';
$p_id = $_GET['name'];
try {
$conn = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
$sql = "SELECT customer.fname, customer.lname, customer.gender, services.treatment, services.date
FROM customer LEFT JOIN services
ON customer.id = services.customer_id
WHERE customer.slug LIKE :id";
$q = $conn->prepare($sql);
$q->execute(array('id' => $p_id));
$q->setFetchMode(PDO::FETCH_ASSOC);
} catch (PDOException $pe) {
die("Could not connect to the database $dbname :" . $pe->getMessage());
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
<div id="container">
<h1>Customer Record</h1>
Name: <br />
Gender: <br />
<table class="table table-bordered table-condensed">
<thead>
<tr>
<th>Customer Name</th>
<th>Customer Gender</th>
<th>Treatment</th>
<th>Date</th>
</tr>
</thead>
<tbody>
<?php while ($r = $q->fetch()): ?>
<tr>
<td><?php echo htmlspecialchars($r['fname']), ' ', htmlspecialchars($r['lname'])?></td>
<td><?php echo htmlspecialchars($r['gender']); ?></td>
<td><?php echo htmlspecialchars($r['treatment']); ?></td>
<td><?php echo htmlspecialchars($r['date']); ?></td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
<a href="search.html">Seach Again</a>
</body>
</div>
</html>
我实现了 SQL Fiddle 的结果,但我想要的是,名称和性别不会一直重复。
我附上截图:
我想要的是根据截图图像,姓名: John Doe 和 性别: 男性,应该在上面,而不是在下表显示时继续重复所有访问细节。我试图修改代码,但似乎并没有真正奏效。
请告诉我,因为我真的不知道如何实现我想要的。
非常感谢。
【问题讨论】: