开发者

getting MySQL results as PHP array

开发者 https://www.devze.com 2023-04-05 21:48 出处:网络
mysql table: +-------------------+----------------+ config_name|config_value| +-------------------+----------------+

mysql table:

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

current php codes:

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

current result:

Array
(
    [0] => Array
        (
            开发者_JAVA百科[config_name] => allow_autologin
            [config_value] => 1
        )

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

)

I want to get the result like that:

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


just add to your results like so:

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


while($r = mysql_fetch_assoc($sth)) {
    $rows[] = array($r['config_name'] => $r['config_value']);
}


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

精彩评论

暂无评论...
验证码 换一张
取 消