dongzaijiao4863 2015-03-20 23:02
浏览 131
已采纳

Php mysql fetch在每行的开头添加行号

This is my php code:

 $query = Select * from tablename;
 $result = mysql_query($query) or die((mysql_error()));
 $count = 0;
 if (mysql_num_rows($result) > 0){


   while( $row = mysql_fetch_array($result)){


      $json_output[$count]=$row;

      $count ++;

   }


}


 $output = json_encode($json_output);

 $output = str_replace('[{', '{', $output);
 $output = str_replace('}]', '}', $output);

 echo $output;

And this is a part of my php output:

 {"0":{"0":"2","Gi_Id":"2","1":"sample_name.jpg","Gi_nome_file":"sample_name.jpg","2":"sample_name","Gi_Pseudonimo":"s. name","3":"sample name","

as you can see, there are the tag "0", "1" , "2", etc that represent the column number ( "0" for Gi_Id , "1" for Gi_nome_file etc) of my sql table. Why there are this duplicates? Probally i can manage this output ignoring the duplicate single cell value, but i want to undestand how to fix that.

I think that the problem is this row of my code:

 $json_output[$count]=$row;

but i don't find another way to get all my table data.

Any suggestions?

EDIT

Using mysql_fetch_assoc i dont have the duplicate but remains the number at the begining of each row, my output is the following:

{"0":{"Gi_Id":"2","Gi_nome_file":"sample_name.jpg","Gi_Pseudonimo":"s. name", 

and for the row number 2 i have:

{"1":{"Gi_Id":"3","Gi_nome_file":"sample_name.jpg","Gi_Pseudonimo":"s. name", 

and for the row number 3 i have:

{"2":{"Gi_Id":"4","Gi_nome_file":"sample_name.jpg","Gi_Pseudonimo":"s. name", 

...

and for the row number n i have:

{"n":{"Gi_Id":"n+1","Gi_nome_file":"sample_name.jpg","Gi_Pseudonimo":"s. name", 

etc i would like to remove the {"0": , {"1": ,{"2":, {"3": at the begining of each row , how i can do this?

  • 写回答

1条回答 默认 最新

  • douba1498 2015-03-21 16:04
    关注

    I solve my problem:

    this is the solution:

     $result = mysql_query($query) or die((mysql_error()));
    

    $json_output = array(); $count = 0;

    $response["Prova"] = array(); if (mysql_num_rows($result) > 0){

    while( $row = mysql_fetch_assoc($result)){
    
    
        $json_output[$count]=$row;
    
        // push single rating into final response array
    
    
        $count ++;
    
    }
    
    array_push($response["Prova"], $json_output);
    
    
    }
    
    //alla fine aggiungo il tag di successo
    $json_output["success"] = 1;
    
    $output = json_encode(array($response));
    //rimuovo le parentesi graffe per far si che non mi dia problemi con il json di android
    $output = str_replace('[{', '{', $output);
    $output = str_replace('}]', '}', $output);
    
    echo $output;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂