【问题标题】:PHP PDO while doesn't achieve my desire end resultPHP PDO 虽然没有达到我想要的最终结果
【发布时间】: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 的结果,但我想要的是,名称和性别不会一直重复。

我附上截图:

Screenshot

我想要的是根据截图图像,姓名: John Doe 和 性别: 男性,应该在上面,而不是在下表显示时继续重复所有访问细节。我试图修改代码,但似乎并没有真正奏效。

请告诉我,因为我真的不知道如何实现我想要的。

非常感谢。

【问题讨论】:

    标签: php mysql pdo


    【解决方案1】:

    由于您在 SQL 查询中执行了LEFT JOIN,因此您提前知道$q-&gt;fetch() 返回的所有fnamelnamegender 值将用于同样 customer.slug,对吗?所以你可以指望它。

    我的建议是改用fetchAll() 函数来获取customer.slug 的所有记录的数组,然后将其呈现在您的视图中。例如(尚未测试过)您可以在$q-&gt;setFetchMode(PDO::FETCH_ASSOC); 之后添加以下内容...

    $cs = $q->fetchAll();   // customer services join
    

    然后,在您的 &lt;html&gt; 视图中,您可以执行以下操作:

    <h1>Customer Record</h1>
    Name: <?php echo htmlspecialchars($cs[0]['fname'].' '.$cs[0]['lname']); ?> <br />
    Gender: <?php echo htmlspecialchars($cs[0]['gender']); ?> <br />
    
    <table>
      <thead>
        <tr>
          <th>Treatment</th>
          <th>Date</th>
        </tr>
      </thead>
      <tbody>
        <?php foreach($cs as $r): ?>
        <tr>
            <td><?php echo htmlspecialchars($r['treatment']); ?></td>
            <td><?php echo htmlspecialchars($r['date']); ?></td>
        </tr>
        <?php endforeach; ?>
    </tbody>
    

    当然,最好检查一下您的查询是否返回了任何条记录,如果没有,则显示“未找到”消息。毕竟,$cs[0] 可能是空的,给你一个 PHP 错误。

    【讨论】:

      猜你喜欢
      • 2022-09-29
      • 1970-01-01
      • 1970-01-01
      • 2016-01-24
      • 2016-03-14
      • 1970-01-01
      • 2011-01-19
      • 1970-01-01
      相关资源
      最近更新 更多