回声超过1个id


Echoing more than 1 id

我想从数据库中回显1个以上的id,如何在索引页中显示1个以上id?

我的功能:

function query($sql) {
 global $conn;
 $results = mysql_query($sql, $conn);
 $rows = array();
 if(!is_bool($results))
     while($row = mysql_fetch_assoc($results)) 
         $rows['id'] = $row;
    return $rows;
}

我的索引页面:

<?php foreach($funds as $fund) { ?>
 <td width="87%" valign="top"><ul id="funds" class="bodycopy2" style="margin:0;">
  <li><?php echo $fund['fund']; ?><span class="price"><div align="center" class="h1"><strong><br />R123.45</strong> <br />as at <br />1 January 2012</div></span></li>
  <li><?php echo $fund['fund']; ?><span class="price"><div align="center"   class="h1">  <strong><br />R200.45</strong> <br />as at <br />1 January 2012</div></span>   </li>
  <li><?php echo $fund['fund']; ?><span class="price"><div align="center" class="h1">   <strong><br />R150.00</strong> <br />as at <br />1 January 2012</div></span></li>
 </td>
     </tr>
     </table>
    <?php } ?>

您可以使用一个id数组,如:

$rows['ids'][] = $row;

然后你可以使用通过ID

foreach ($rows as $id) 

如果只保留id,则直接使用$rows[] = $row

您总是覆盖同一个元素$rows['id'] = $row;。你应该做

$rows['id'][] = $row;

要显示id,您应该在$rows 上迭代

foreach($rows['id'] as $id){
    echo $id;
}