簡體   English   中英

PHP中的2D數組

[英]2D array in PHP

如何將$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 userId | name | dob userId | name | dob

像這樣:

$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...

如果您不希望為子數組使用關聯數組,那么還有其他獲取模式

$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”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM