使用id作为键,并将name命名为数组的值

时间:2012-01-06 13:56:14

标签: php mysql

我想更改mysql结果的格式......

[0] =>  
      [id]   => 1    
      [name] => King     
[1] =>  
      [id]   => 5
      [name] => Queen  

采用以下格式:

[1] => King
[5] => Queen 

但我不知道如何将mysql结果放入这种格式。

$teamSql   = "SELECT id, name FROM users WHERE  projectid = '1';
$teamQuery = mysql_query($teamSql);
$teamFetch= mysql_fetch_assoc($teamQuery);
// and now?

2 个答案:

答案 0 :(得分:5)

$teamSql   = "SELECT id, name FROM users WHERE  projectid = '1'";
$teamQuery = mysql_query($teamSql);
$teams = array();
while ($team = mysql_fetch_array($teamQuery))
{
    $teams[$team['id']] = $team['name'];
}

答案 1 :(得分:-1)

用于在PHP中显示结果,

使用foreach循环

像foreach一样($ teamFetch为$ teamFetched){

echo $ teamFetched;

}

多数民众赞成......