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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害