在PHP中传递多维数组

时间:2013-10-05 09:28:56

标签: php mysql

MySql查询返回一个多维数组:

function d4g_get_contributions_info($profile_id)
{
   $query = "select * from contributions where `project_id` = $profile_id";
   $row = mysql_query($query) or die("Error getting profile information , Reason : " .    mysql_error());
   $contributions = array();
   if(!mysql_num_rows($row)) echo "No Contributors";

   while($fetched = mysql_fetch_array($row, MYSQL_ASSOC))
   {
       $contributions[$cnt]['user_id'] = $fetched['user_id'];
       $contributions[$cnt]['ammount'] = $fetched['ammount'];
       $contributions[$cnt]['date'] = $fetched['date'];
       $cnt++;  
   }
   return $contributions;
}

现在我需要在我调用此函数的页面中打印值。我该怎么做?

1 个答案:

答案 0 :(得分:2)

改变这样的功能:

while($fetched = mysql_fetch_array($row, MYSQL_ASSOC))
{
$contributions[] = array('user_id' => $fetched['user_id'],
                            'ammount' => $fetched['ammount'],
                            'date' => $fetched['date']);

}

return $contributions;

然后尝试以下:

$profile_id = 1; // sample id
$result = d4g_get_contributions_info($profile_id);
foreach($result as $row){
    $user_id = $row['user_id']
    // Continue like this
}