dongmo1708 2015-07-11 14:08
浏览 59

使用OpenOffice制作csv文件并使用php转储csv数据

I have been following a csv import tutorial with PHP using mysql to store data, when I echo the data, I am getting the whole row instead of getting the data in the first cell. Could this be an issue with my csv file because my field delimited is: comma and my text delimited is: double quotes? when I echo out the first element echo $fileop[0];, I get the whole row instead of getting the first element which should be: title. Anyone would know what is the issue?

Here is my code:

<?php 

    $host = 'localhost';
    $user = 'root';
    $pass = 'root';
    $db_name = 'testcsvdump';

    $db = new mysqli($host, $user, $pass, $db_name);

    if($db->connect_errno > 0){
        die('Unable to connect to database [' . $db->connect_error . ']');
    }


    if( isset($_POST['submit']) ){

        $file = $_FILES['file']['tmp_name'];
        $handle = fopen($file, "r");

        while ( ($fileop = fgetcsv($handle, 1000, ',')) !== false ) {
            echo $fileop[0];
        }

    }else{
            echo 'failed';
        }


?>

and the output is:

titleNameEmail Address

(but I am trying to echo out "title" which is the first element in the first cell). And when I open my CSV file, each title and data are in their own cell. I used the following settings to export the file.

Field delimited is: comma

Text delimited is: double quotes

  • 写回答

2条回答 默认 最新

  • dqq9695 2015-07-11 20:43
    关注

    The issue was with the csv file itself, when I opened the csv with a text edito, I noticed that the end of each row did not have a comma so when I tried to echo the first element, the script was echoing out the first row. Once I added the missing commas in the csv file, the php script did exactly what it was supposed to do.

    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line