【问题标题】:Displaying different values from database in different columns from same table在同一个表的不同列中显示来自数据库的不同值
【发布时间】:2012-06-15 05:48:00
【问题描述】:

以下是我的代码,我试图在其中显示来自同一个表但在具有不同 ID 的 HTML 表的不同列中的值,例如假设我的表有三列,然后在每一列中显示不同的值

我只希望我各自的 id 在同一张表中显示的数据的每一列上发生变化

类似于:ID1 的值 | ID2 的值 | ID3的值

但目前我的工作时间是这样的: ID1 的值 | ID1 的值 | ID1的值

请告诉我如何修复以下代码:

          <table width="714" height="318" border="0" align="right" cellpadding="2" cellspacing="0">
          <?php $count=0; do { 
                  { ++$count;

          ?> <tr>
            <td width="231" height="288" valign="top" background="images/viv.png"><div align="center"><img src="get.php?id=<?php echo $row_Recordset1['ID']; ?>"  width="270" height="270" /><br />
                <br />
              <a href="get_deal.php?id=<?php echo $row_Recordset1['ID']; ?>"><img src="button.png" width="182" height="36" border="0" /></a><br />
              <br />
            </div></td>
            <td width="231" valign="top" background="images/viv.png"><div align="center"><img src="get.php?id=<?php echo $row_Recordset1['ID']; ?>"  width="270" height="270" /><br />
                    <br />
                    <a href="get_deal.php?id=<?php echo $row_Recordset1['ID']; ?>"><img src="button.png" width="182" height="36" border="0" /></a><br />
                    <br />
            </div></td>
            <td width="231" valign="top" background="images/viv.png"><div align="center"><img src="get.php?id=<?php echo $row_Recordset1['ID']; ?>"  width="270" height="270" /><br />
                    <br />
                    <a href="get_deal.php?id=<?php echo $row_Recordset1['ID']; ?>"><img src="button.png" width="182" height="36" border="0" /></a><br />
                    <br />
            </div></td>
          </tr>
              <tr>
                <td height="23" valign="top">&nbsp;</td>
                <td valign="top">&nbsp;</td>
                <td valign="top">&nbsp;</td>
              </tr>
              <?php }

              } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1));
if ($count == 0)
            echo '<span style="font-family:Tahoma;color:white;font-size:200%">No Deals Available</span>';   ?>
        </table></td>
      </tr>
    </table>

解决方案: http://www.qualitycodes.com/tutorial.php?articleid=33&title=Displaying-Records-in-Multiple-Columns-Using-PHP

【问题讨论】:

  • 如果您使用表格显示,为什么不使用 而不是
  • 您确定这按您的预期工作吗?似乎对于循环的第一次迭代,$row_Recordset1 不会被设置,你最终会得到像&lt;a href="get_deal.php?id="&gt; 这样的链接。这是因为您使用的是 do{...} while() 循环而不是标准的 while(){...} 循环。

标签: php sql database


【解决方案1】:

您一直在回显$row_Recordset1['ID'];,这意味着您一直在显示 ID 列。您必须将ID 更改为您要显示的列的名称。

在这种情况下,他们继续使用 ID1,因此如果所有三个值都是 ID,那么您需要在选择中使用别名(称它们为不同的名称),以便您可以使用 ID1ID2ID3

例如,如果您当前的 sql 查询是:

select *
from tablex.....

然后你现在必须提取列并命名它们:

select tablex.ID as ID1, secondTable.ID as ID2, thirdTable.ID as ID3
from tablex.....

然后您可以将它们称为ID1ID2ID3

【讨论】:

  • hmmm 是的,你是对的,这三个都是 ID,所以创建一个别名将解决问题,你能提示一下这样的查询吗
  • 这取决于您的查询。但总体思路是使用AS 关键字。例如:SELECT table1.ID AS ID1, table2.ID AS ID2...
  • 表格是一样的。所以在那种情况下,如果我这样做table1.ID AS ID1, table1.ID As ID2 ,我认为它不会显示不同的值
  • 为什么你的表中有3次相同的列名?您应该始终使用不同的列名。因此,数据库可能只是获取第一个 ID 而看不到其他 ID。
  • 我怎样才能创建这样的脚本,在第二列 id 发生变化?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-23
  • 2020-10-14
  • 2011-09-13
  • 1970-01-01
  • 2020-09-27
  • 1970-01-01
相关资源
最近更新 更多