Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在CSV文件的末尾附加括号_Php_Mysql_Csv - Fatal编程技术网

Php 在CSV文件的末尾附加括号

Php 在CSV文件的末尾附加括号,php,mysql,csv,Php,Mysql,Csv,在内容的开头和结尾,我必须附加括号,我的csv文件如下所示 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 14276538

在内容的开头和结尾,我必须附加括号,我的csv文件如下所示

        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 
添加括号后,内容应如下所示:[1427653800,95,65160] 我的php代码如下:

    <?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

这是我的conn.php文件,请给我一些建议

尝试在括号中添加和前置:

$row[0] = '['.$row[0];
$row[3] .= ']';
fputcsv($fp, $row);

尝试使用my函数,该函数将文本添加到关联数组的第一个和最后一个元素:

<?php

function array_brackets($array,$prefix,$suffix){

//add prefix to the first element
end($array);
$key = key($array);
$array[$key] = $prefix . $array[$key];

//add sufix to the last element
reset($array);
$key = key($array);
$array[$key] = $array[$key] . $suffix;

return $array;
}


$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, array_brackets($row,"[","]"));

}

fclose($fp);

?> 

请运行此代码并给出输出:

<?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, array_brackets($row,"[","]"));
  print_r($row);
  exit;
}

//fclose($fp);

?> 

最好使用此查询

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

它会给你类似于[1427653800,95,65160]的结果,所以在循环时不需要在你身上做任何代码

@satish,好的会试试@akram好的会试试看@akram如果我把,$row[0]='['.$row[0];$row[3].='];fputcsv($fp,$row);在while循环中,我获取输出为date1,成功,失败,计数1427653800,95,65160,[,],但是如果我在while循环中使用“[”$row.”,我需要这样的[1427653800,95,65160]@satish,解析错误:语法错误,在C:\wamp\www\highcharts1\Conn.php的第40行@Miglen中出现意外的“}”,我在C:\wamp\www\\StatisticsDisplay\highcharts1\Conn.php的第37行得到了未定义的索引:date1,date1,success,failure,count 1427653800,95,65160],[我得到的是这样而不是[1427653800,95,65160]请尝试让我知道!!@satish$row是一个数组,它不起作用。可以在这里找到示例:如果它适合您,请给我一行。@user3753411您能执行并给我输出吗: