【问题标题】:PHP / MySQL Joining - Nested While?PHP / MySQL 加入 - 嵌套时?
【发布时间】:2014-08-27 01:12:05
【问题描述】:

关注我的previous question...

我正在尝试让我的 CustomerName ID 转换完全正常工作,并且它在一段时间循环中在我的显示/编辑页面上运行良好,但我很困惑如何让它在我的项目列表页面上运行。

例如,以下代码将显示我的 Projects 表中的所有项目,使用 CustomerID 而不是 CustomerName。我有从 CustomerID 转换为 CustomerName 的 SQL,但是我到底要如何将它注入到这段代码中呢?

项目列表

<?php
include_once('includes/config.inc.php');
if ($connection->connect_errno) {
    echo "Failed to connect to MySQL: (" . $connection->connect_errno . ") " . $connection->connect_error;
}

$result = $connection->query("SELECT * FROM Projects");;

$CustomerNameResult = $connection->query("SELECT CustomerName FROM Customers JOIN Projects ON Projects.CustomerID = Customers.ID");;

echo "<table width='100%' border='1'>
<tr>
<th>Project ID</th>
<th>Customer Name</th>
<th>Summary</th>
<th>Phase</th>
</tr>";

while($row = mysqli_fetch_array($result)) {
  echo "<tr>";
  echo "<td><p class='content'><a href='display_project.php?ID=" . $row['ID'] . "'>" . $row['ID'] . "</p></td>";
  echo "<td><p class='content'>" . $row['CustomerID'] . "</p></td>";
  echo "<td><p class='content'>" . $row['Summary'] . "</p></td>";
  echo "<td><p class='content'>" . $row['Phase'] . "</p></td>";
  echo "</tr>";
}

echo "</table>";

mysqli_close($connection);
?>

我尝试在其中嵌套另一个 while 循环,但它只会在第一列中造成混乱,实际上并没有将它们放在它们所属的位置。

提前感谢任何 cmets 或答案!

【问题讨论】:

    标签: php mysql join while-loop


    【解决方案1】:
    $result = $connection->query("SELECT * FROM Projects JOIN Customers ON Projects.CustomerID = Customers.ID");
    

    你将拥有客户和项目的所有数据,你可以在获取后使用......并且你可以删除$CustomerNameResult

    【讨论】:

      猜你喜欢
      • 2011-12-05
      • 2017-03-26
      • 2018-12-29
      • 1970-01-01
      • 1970-01-01
      • 2017-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多