dongman5539 2019-03-01 17:09
浏览 108

在php中创建csv文件,将每行放在一个新行上

I am using the fwrite() function in PHP to create a csv file. It connects to a database and retrieves two columns from a table. I can get this to work, however it only writes the first row. For example..

+------+-----+--+--+--+ | John | Doe | | | | +------+-----+--+--+--+ | | | | | | +------+-----+--+--+--+ | | | | | | +------+-----+--+--+--+

How do i get it to go onto the next line and write the next row, and keep on writing how ever many rows there are in the table. For example..

+---------+-----+--+--+--+ | John | Doe | | | | +---------+-----+--+--+--+ | Jane | Doe | | | | +---------+-----+--+--+--+ | Richard | Doe | | | | +---------+-----+--+--+--+ | | | | | | +---------+-----+--+--+--+

Here is my PHP code

fwrite($file, ucfirst($row['name']) . ', ' . ucfirst($row['visiting']));
  • 写回答

1条回答 默认 最新

  • duanchu2607 2019-03-01 17:15
    关注

    Use fputcsv instead - it will take care of the formatting for you!

    http://php.net/manual/en/function.fputcsv.php

    fputcsv( $file, array( ucfirst($row['name']), ucfirst($row['visiting']) ) );
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于Lammps建模的描述
  • ¥15 #lingo#请问一下为什么会出现以下情况,是因为l第一个值是0的缘故吗?
  • ¥15 设计格雷码同步八进制计数器
  • ¥100 改写matlab程序(关键词-系统对)
  • ¥15 函数信号发生器仿真电路
  • ¥15 Qt的pixmap和image图片加载都导致程序崩溃怎么办
  • ¥15 Kali木马生成问题求解
  • ¥30 求一下解题思路,完全不懂
  • ¥15 C51单片机串口控制JQ6500语音模块
  • ¥30 想给yolo5模型加一个图片识别界面,但是图片还没有检测出来就闪退了