我的数据库表中有两个条目,但由于某种原因,我的PHP / SQL显示了每个条目的6个重复项。
<?
mysql_select_db($database_database_connection, $database_connection);
$getdevices=("SELECT * FROM Device_tbl");
$getdevicesresult = mysql_query($getdevices, $database_connection) or die(mysql_error());
while($device=mysql_fetch_assoc($getdevicesresult))
{
foreach($device as $devicevalue) { ?>
<div class="hold-cont">
<div class="holder">
<div class="image-hold" >
<img class="image-icon" src="images/android.png"/>
</div>
</div>
<div class="device-name devicename-txt"><? print_r($device['Model']); ?></div>
</div>
<? }}
mysql_close();
?>
这应该只返回2行,而不是每行6。有什么想法吗?
答案 0 :(得分:3)
您将循环两次结果。摆脱不必要的foreach()
调用(while()
将遍历查询的所有结果。)
<?php
mysql_select_db($database_database_connection, $database_connection);
$getdevices=("SELECT * FROM Device_tbl");
$getdevicesresult = mysql_query($getdevices, $database_connection) or die(mysql_error());
while($device=mysql_fetch_assoc($getdevicesresult)){
?>
<div class="hold-cont">
<div class="holder">
<div class="image-hold" >
<img class="image-icon" src="images/android.png"/>
</div>
</div>
<div class="device-name devicename-txt"><?php echo $device['Model']; ?></div>
</div>
<?php
}
mysql_close();
?>
注意: 我还将print_r(...)
更改为echo ...
,因为$device['Model']
不是数组而我已将您的空缺标记(<?
)更改为正常标记(<?php
),因为并非所有服务器都支持short tags
(请参阅Are PHP short tags acceptable to use?)。
答案 1 :(得分:0)
请更正以下内容
<div class="device-name devicename-txt"><? print_r($device['Model']); ?></div>
与
<div class="device-name devicename-txt"><? echo($devicevalue['Model']); ?></div>