简体   繁体   English

PHP中的2D数组

[英]2D array in PHP

how can I store $result value in a 2d array. 如何将$result值存储在二维数组中。 here my code- 这是我的代码-

$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 array having three columns- userId | name | dob 具有三列的2d数组-userId userId | name | dob userId | name | dob

Something like this: 像这样:

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

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

That will give you: 那会给你:

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

If you don't want an associate array for the sub-array, there's other fetch modes as well 如果您不希望为子数组使用关联数组,那么还有其他获取模式

$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";

Then we use mysql_fetch-assoc to collect a row 然后我们使用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'];

Also added "LIMIT 1" to your query 还向您的查询添加了“ LIMIT 1”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM