从存储MySQL过程导出PHP CSV

从存储MySQL过程导出PHP CSV,php,mysql,csv,stored-procedures,Php,Mysql,Csv,Stored Procedures,尝试使用下面的代码调用MySQL上的存储过程并导出到CSV。它生成一个csv文件,但该文件为空。存储过程称为DATAEXPORT1 任何想法都将不胜感激 <?php $host = "localhost"; $username = "xxx"; $password = "xxx"; $dbname = "xxx"; $file = 'export'; $connection = mysqli_connect($host, $username, $password, $dbname)

尝试使用下面的代码调用MySQL上的存储过程并导出到CSV。它生成一个csv文件,但该文件为空。存储过程称为DATAEXPORT1

任何想法都将不胜感激

<?php


 $host = "localhost";
$username = "xxx";
$password = "xxx";
$dbname = "xxx";
$file = 'export';
$connection = mysqli_connect($host, $username, $password, $dbname) or     die("Connection Error " . mysqli_error($connection));
/*
* execute sql query
*/

$sql = "Call DATAEXPORT1()";

$result = mysqli_query($connection,$sql) or die("Selection Error " . mysqli_error($connection));

if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$csv_output .= $row['Field'].", ";
$i++;
}
}
$csv_output .= "\n";

$values = mysqli_query($connection,$sql);
while ($rowr = mysqli_fetch_row($values)) {
for ($j=0;$j<$i;$j++) {
 $csv_output .= $rowr[$j].", ";
}
 $csv_output .= "\n";
 }


$filename = $file."_".date("Y-m-d_H-i",time());
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header("Content-disposition: filename=".$filename.".csv");
print $csv_output;
mysqli_close($connection);
exit;
?>

使用了以下代码,效果很好:

 <?php
 $host = "localhost";
$username = "xxx";
$password = "xxx";
$dbname = "xxx";
 $connection = mysqli_connect($host, $username, $password, $dbname) or die("Connection Error " . mysqli_error($connection));

// fetch mysql table rows
$sql = "CALL DATAEXPORT1()";
$result = mysqli_query($connection, $sql) or die("Selection Error " . mysqli_error($connection));

header('Content-type: text/csv');
header('Content-Disposition: attachment; filename="output1.csv"');

$file = fopen('php://output', 'w');


while($row = mysqli_fetch_assoc($result))
{
    fputcsv($file, $row);
}

fclose($file);

//close the db connection
mysqli_close($connection);
exit();

使用了以下代码,效果很好:

 <?php
 $host = "localhost";
$username = "xxx";
$password = "xxx";
$dbname = "xxx";
 $connection = mysqli_connect($host, $username, $password, $dbname) or die("Connection Error " . mysqli_error($connection));

// fetch mysql table rows
$sql = "CALL DATAEXPORT1()";
$result = mysqli_query($connection, $sql) or die("Selection Error " . mysqli_error($connection));

header('Content-type: text/csv');
header('Content-Disposition: attachment; filename="output1.csv"');

$file = fopen('php://output', 'w');


while($row = mysqli_fetch_assoc($result))
{
    fputcsv($file, $row);
}

fclose($file);

//close the db connection
mysqli_close($connection);
exit();

我只对您的脚本进行了少量修改

<?php
$host     = "localhost";
$username = "xxx";
$password = "xxx";
$dbname   = "xxx";

$file     = 'export';

$connection = mysqli_connect($host, $username, $password, $dbname) or     die("Connection Error " . mysqli_error($connection));

/*
 * execute sql query
 */

$sql = "Call DATAEXPORT1()";

$result = mysqli_query($connection,$sql) or die("Selection Error " . mysqli_error($connection));

$num_rows = count($result);
$csv_output = '';

if( $num_rows > 0) {
    foreach($result as $item => $value){
        $csv_output .= $item . ", " . $value . "\n";
        $i++;
    }
}


$filename = $file . "_" . date("Y-m-d_H-i",time());

header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header("Content-disposition: filename=".$filename.".csv");

print $csv_output;

mysqli_close($connection);

exit;
?>

我只对您的脚本进行了少量修改

<?php
$host     = "localhost";
$username = "xxx";
$password = "xxx";
$dbname   = "xxx";

$file     = 'export';

$connection = mysqli_connect($host, $username, $password, $dbname) or     die("Connection Error " . mysqli_error($connection));

/*
 * execute sql query
 */

$sql = "Call DATAEXPORT1()";

$result = mysqli_query($connection,$sql) or die("Selection Error " . mysqli_error($connection));

$num_rows = count($result);
$csv_output = '';

if( $num_rows > 0) {
    foreach($result as $item => $value){
        $csv_output .= $item . ", " . $value . "\n";
        $i++;
    }
}


$filename = $file . "_" . date("Y-m-d_H-i",time());

header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header("Content-disposition: filename=".$filename.".csv");

print $csv_output;

mysqli_close($connection);

exit;
?>


您不喜欢fputcsv()或introde(),因为…?因为我尝试使用另一个包含fputcsv()的脚本,但遇到了相同的问题@SYMCBEAN您不喜欢fputcsv()或内爆(),因为…?因为我尝试使用另一个包含fputcsv()的脚本,但遇到了相同的问题@符号bean