使MySQL结果PHP数组 [英] getting MySQL results as PHP array

查看:105
本文介绍了使MySQL结果PHP数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

mysql表:

  + ------------------- + ---------------- +
| CONFIG_NAME | CONFIG_VALUE |
------------------- + + + ----------------
| allow_autologin | 1 |
| allow_md5 | 0 |
------------------- + + + ----------------

当前的PHP codeS:

  $某物=的mysql_query(SELECT ...);
$行=阵列();
而($ R = mysql_fetch_assoc($某物)){
    $行[] = $ R;
}
的print_r($行);

目前的结果是:

 阵列

    [0] =>排列
        (
            [CONFIG_NAME] => allow_autologin
            [CONFIG_VALUE] => 1
        )    [1] =>排列
        (
            [CONFIG_NAME] => allow_md5
            [CONFIG_VALUE] => 0
        ))

我想要得到这样的结果:

 阵列(allow_autologin =大于1,allow_md5 = 0)


解决方案

只是添加到您的结果,像这样:

  $某物=的mysql_query(SELECT ...);
$行=阵列();
而($ R = mysql_fetch_assoc($某物)){
    $行[$ R ['CONFIG_NAME'] = $ R ['CONFIG_VALUE'];
}
的print_r($行);

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
        (
            [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);

这篇关于使MySQL结果PHP数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆