dongpu9481 2015-04-07 10:24
浏览 53
已采纳

在CSV文件的末尾附加括号

At the beginning and at ending of the contents I have to append the brackets my csv file looks like this

        date1,success,failure,count
        1427653800,95,65,160
        1427653800,30,10,40
        1427740200,10,8,18
        1427740200,30,38,68
        1427826600,38,20,58
        1427826600,60,10,70
        1427653800,15,15,30
        1427653800,10,10,20 

After adding brackets the contents should look like this: [1427653800,95,65,160] My php code is below:

    <?php
        $list = array ('date1', 'success', 'failure','count');

        $sql = "SELECT (SUBSTRING(UNIX_TIMESTAMP(date1),1,10)),success,failure,count from h_statistics;";
              $users_profile_user_id = mysqli_query($conn, $sql); 

          $fp = fopen("data.csv", "w");

            fputcsv($fp, $list);
        while($row = mysqli_fetch_array($users_profile_user_id, MYSQLI_ASSOC))
        {

          fputcsv($fp, $row);

        }

        fclose($fp);
    ?> 

this is my conn.php file ,please suggest me on this
  • 写回答

4条回答 默认 最新

  • dtt27783 2015-04-07 11:13
    关注

    it is better to use this query

     $sql = "SELECT CONCAT('[','',(SUBSTRING(UNIX_TIMESTAMP(date1),1,10))),success,failure,CONCAT(count,'',']') from h_statistics";
    

    it give you result like [1427653800,95,65,160] so no need to do any code in you while loop

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

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c