2011-09-18 111 views
2

MySQL表:獲得MySQL的結果PHP數組

+-------------------+----------------+ 
| config_name  | config_value | 
+-------------------+----------------+ 
| allow_autologin |  1  | 
| allow_md5   |  0  | 
+-------------------+----------------+ 

當前的PHP代碼:

$sth = mysql_query("SELECT ..."); 
$rows = array(); 
while($r = mysql_fetch_assoc($sth)) { 
    $rows[] = $r; 
} 
print_r($rows); 

當前的結果:

Array 
(
    [0] => Array 
     (
      [config_name] => allow_autologin 
      [config_value] => 1 
     ) 

    [1] => Array 
     (
      [config_name] => allow_md5 
      [config_value] => 0 
     ) 

) 

我想要得到這樣的結果:

Array(allow_autologin => 1, allow_md5 => 0) 

回答

5

只需添加到您的結果,像這樣:

$sth = mysql_query("SELECT ..."); 
$rows = array(); 
while($r = mysql_fetch_assoc($sth)) { 
    $rows[$r['config_name']] = $r['config_value']; 
} 
print_r($rows); 
+0

肯定。有用。非常感謝你。完美的幫助。 A +。謝謝。我得等7分鐘來選擇你的答案.. –

4
while($r = mysql_fetch_assoc($sth)) { 
    $rows[] = array($r['config_name'] => $r['config_value']); 
} 
1
$sth = mysql_query("SELECT ..."); 
$rows = array(); 
while($r = mysql_fetch_assoc($sth)) { 
    $rows[$r['config_name']] = $r['config_value']; 
} 
print_r($rows);