php 存储数据在数组的问题

来源:百度知道 编辑:UC知道 时间:2024/05/27 10:58:40
我想从数据库中读取id、name 两个个字段的数据
然后存放在数组中。数据库中的数据如下:
id name
1 aa
2 bb
. ..
请问。
1、在php 数组中如何存储id、name的数据
2、存储后如何读取出来,显示在网页上?

<?php
$arr=array();//空的数组
$sql='select id,name from tab';
$res=mysql_query($sql);
while($row=mysql_fetch_array($res)) $arr[]=$row;//添加一行数据到数组里面
mysql_free_result($res);

foreach ($arr as $row) echo $row['id'].' '.$row['name'].'<br>';//显示数组
?>