dsc56927 2016-04-02 14:51
浏览 19

fputcsv适用于MySQL,不适用于SQL Server(导出CSV)

I have identical tables in MySQL and Microsoft SQL Server:

  • ID = 1
  • Column1 = Hello world
  • Column2 = How are you today

I am able to use the PHP fputcsv function to display the data from MySQL in a Web browser. To isloate the cause of the problem I am facing, I have commented out the header, to force the fputcsv function to display the data in the Web browser instead of the CSV file. The following PHP will produce the following output in a Web browser:

<?php
//header("Content-Type: text/csv;charset=utf-8");

$con = new mysqli('domain','username','password','database');
$select = "select * from tablename";
$select_query = mysqli_query($con, $select);
$fp= fopen('php://output', 'w');

while ($row = mysqli_fetch_array($select_query,MYSQL_ASSOC)) {
fputcsv($fp, array_values($row));
}

fclose($fp);
?>

enter image description here

When using similar PHP to display data in a Microsoft SQL Server table using the fputcsv function, the Web browser display a 500 error such as Page cannot be displayed.

<?php
//header("Content-Type: text/csv;charset=utf-8");

$ServerName = "domain";
$ConnectionString = array("Database"=>"Database", "UID"=>"username", "PWD"=>"password");
$con = sqlsrv_connect($ServerName, $ConnectionString);
$select = "select * from tablename";
$select_query = sqlsrv_query($con, $select);
$fp= fopen('php://output', 'w');

while ($row = sqlsrv_fetch_array($select_query, SQLSRV_FETCH_ASSOC)) {
fputcsv($fp, array_values($row));
}

fclose($fp);
?>

Inside the while loop, if I replace fputcsv($fp, array_values($row)); with echo $row['column1'];, the 500 error no longer appears, and Hello world is displayed. This tells me the while loop is able to fetch the array, and there is some issue with this line of code when using SQL Server:

fputcsv($fp, array_values($row));

I noticed this post on StackOverflow uses array_keys and a foreach loop instead of array_values. When I use array_keys and a foreach loop, the SQL Server table columns are displayed.

while ($row = sqlsrv_fetch_array($select_query, SQLSRV_FETCH_ASSOC)) {
  fputcsv($fp, array_values($row));
    foreach ($row as $data) {
       fputcsv($fp, $data);
    }
}

enter image description here

I noticed the first answer at this post on StackOverflow also uses array_keys instead of array_values. This gives me assurance that array_key should be used with SQL Server.

I am not certain why the Web browser is displaying the column names instead of the data when using array_keys and SQL Server. If there are any tips or suggestions or thoughts that might lead me to my next troubleshooting step here, I will definitely be appreciative.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
    • ¥15 C# datagridview 单元格显示进度及值
    • ¥15 thinkphp6配合social login单点登录问题
    • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
    • ¥15 如何在scanpy上做差异基因和通路富集?
    • ¥20 关于#硬件工程#的问题,请各位专家解答!
    • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配