douxi9245 2017-02-22 08:26
浏览 117
已采纳

为什么mysql_fetch_array会输出多个重复的结果

From this simple query, I could not figure out why I am getting the repeated result one in [0] and another in [m_id]. I believe the result from given query must display only two values.

$sql="SELECT m.m_id, m.work FROM mun as m WHERE m.mun_id=7 ";
$rslt=mysql_query($sql);
$result=mysql_fetch_array($rslt);
print_r($result);

Output:

Array
    (
        [0] => 7
        [m_id] => 7
        [1] => 260
        [work] => 260
    )

Can somebody figure me out what I am doing wrong. Thank you.

  • 写回答

2条回答 默认 最新

  • doujiaoben28596 2017-02-22 08:28
    关注

    That's the normal behavior of mysql_fetch_array. It provides both numerical and associative indices.

    If you want only one of them, use mysql_fetch_assoc or mysql_fetch_row

    $result=mysql_fetch_row($rslt);
    
    Array
        (
            [0] => 7
            [1] => 260
        )
    

    or

    $result=mysql_fetch_assoc($rslt);
    
    Array
        (
            [m_id] => 7
            [work] => 260
        )
    

    It's also worth mentioning that you can get this behavior using mysql_fetch_array by passing a second argument.

    // same as mysql_fetch_row
    $result=mysql_fetch_array($rslt, MYSQL_NUM);
    

    and

    // same as mysql_fetch_assoc
    $result=mysql_fetch_array($rslt, MYSQL_ASSOC);
    

    More info: http://php.net/manual/en/function.mysql-fetch-array.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建