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

    报告相同问题?

    悬赏问题

    • ¥80 用PE可以更改原win11系统的环境变量吗?
    • ¥20 win10商店接入问题
    • ¥15 java 这种树形框吗
    • ¥40 找同学帮敲Python代码
    • ¥15 MYSQL 订单的商品明细重复计算问题
    • ¥15 微信实时共享位置修改
    • ¥100 TG的session协议号转成直登号号后客户端登录几分钟后自动退出设备
    • ¥50 共模反馈回路的小信号增益
    • ¥15 arduino ssd1306函数与tone函数放歌代码不兼容问题
    • ¥70 0.96版本hbase的row_key里含有双引号,无法deleteall