PHP中的2D数组

时间:2010-06-30 06:03:18

标签: php multidimensional-array

如何在{2}数组中存储$result值。这是我的代码 -

$sql="SELECT a.userId, b.name, b.dob FROM tbltree a INNER JOIN tblprofile b ON a.userId = b.userId WHERE a.superId ='$uid'";
$result=mysql_query($sql,$link)or die(mysql_error());

2d数组有三列 - userId | name | dob

2 个答案:

答案 0 :(得分:5)

这样的事情:

$sql = "..."
$result = mysql_query(...) ...;

$result_array = array();
while($row = mysql_fetch_assoc($result)) {
    $result_array[] = $row;
}

那会给你:

$result_array[0] = array('key1' => 'val1', 'key2' => 'val2', ...);
$result_array[1] = array('key1' => 'val1', 'key2' => 'val2', ...);
$result_array[2] = etc...

如果您不想要子阵列的关联数组,那么还有其他fetch modes

答案 1 :(得分:0)

$sql = "SELECT a.userId, b.name, b.dob FROM tbltree a INNER JOIN tblprofile b ON a.userId = b.userId WHERE a.superId ='$uid' LIMIT 1";

然后我们使用mysql_fetch-assoc来收集行

if(false != ($resource = mysql_query($sql)))
{
    $result = mysql_fetch_assoc($resource);
    // Dont use a while here as we only want to iterate 1 row.
}

echo $result['name'];

还在查询中添加了“LIMIT 1”