driuwt9557 2013-07-29 09:50
浏览 45
已采纳

mysql_fetch_array获取日期两次获取每个元素

i hope to iterate the elements fetched from datebase
But the result looks very unexpected .
I found the code below print the $value and echo "<td id=".$key.$tag.">".$value."</td>";twice. Is there anything i misunderstood?

   function selectTable($table){
        $sql= "SELECT * FROM ".$table ;
         $result=mysql_query($sql)
                          or die(mysql_error());
        return $result;
  }

  $table = 'battery_con';
  $result = selectTable($table);
  unset($table);

 while($row = mysql_fetch_array($result)){
        ......
        foreach ($row as $key => $value) {
                  print $value; 
                  echo "<td id=".$key.$tag.">".$value."</td>";
                }       
        .....
    }
  • 写回答

3条回答 默认 最新

  • dongtan7639 2013-07-29 10:20
    关注

    You are using mysql_fetch_array which by default returns an array with two elements per column (this is what the second (optional) paramter means: result_type = MYSQL_BOTH).

    One is indexed with an integer representing the column index, one is indexed with the column name.

    That's why you get two entries in your list. You would set the second parameter to MYSQL_ASSOC to get just one value per column.

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

报告相同问题?

悬赏问题

  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图