Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/285.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_Database_Csv_Hyperlink - Fatal编程技术网

PHP在数据库中回显表,并将其作为CSV文件下载

PHP在数据库中回显表,并将其作为CSV文件下载,php,database,csv,hyperlink,Php,Database,Csv,Hyperlink,我是PHP新手,正在尝试创建一个小代码片段来读取数据库中的表,并允许用户将表下载到CSV文件中 到目前为止,我已经能够连接到我的数据库并通过表进行回显 // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed1: " . $conn-&

我是PHP新手,正在尝试创建一个小代码片段来读取数据库中的表,并允许用户将表下载到CSV文件中

到目前为止,我已经能够连接到我的数据库并通过表进行回显

 // Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
die("Connection failed1: " . $conn->connect_error);
} 

// SQL query
$sql = "SHOW TABLES IN `abc1`";

// perform the query and store the result
$result = $conn->query($sql);

// if the $result not False, and contains at least one row
     if($result !== false) {

       // if at least one table in result
       if($result->num_rows > 0) {
       // traverse the $result and output the name of the table(s)
            while($row = $result->fetch_assoc()) {
                 echo '<br />'. $row['Tables_in_abc1'];
            }
       }
 else echo 'There is no table in "tests"';
 }
 else echo 'Unable to check the "tests", error - '. $conn->error;


 $conn->close();
 ?>
//创建连接
$conn=newmysqli($servername、$username、$password、$dbname);
//检查连接
如果($conn->connect\u错误){
die(“连接失败1:.$conn->连接错误”);
} 
//SQL查询
$sql=“在'abc1'中显示表格”;
//执行查询并存储结果
$result=$conn->query($sql);
//如果$result不为False,并且至少包含一行
如果($result!==false){
//如果结果中至少有一个表
如果($result->num_rows>0){
//遍历$result并输出表的名称
而($row=$result->fetch_assoc()){
回显“
”。$row['Tables_in_abc1'”; } } else echo“tests”中没有表”; } else echo“无法检查“测试”,错误-”$连接->错误; $conn->close(); ?>
现在我想把每个表变成一个链接,这样当用户点击它时,他们就可以将表的数据下载到CSV文件中


我该怎么做

这应该是一个评论,但我的级别不够高,不能留下评论。您应该查看PHPExcel


它附带了许多例子,可以帮助你实现你想要做的事情。

这应该是一个评论,但我的级别不够高,不能留下一个。您应该查看PHPExcel


它附带了许多示例,可以帮助您实现所要做的事情。

您可以将数据流式传输到客户端,如下所示:

header('Content-type: text/csv');
header('Content-disposition: attachment;filename=file.csv');

$stdout = fopen('php://stdout', 'w');
while($row = $result->fetch_assoc()) {
    fputcsv($stdout, $row);
}
fclose($stdout);
或写入文件:

$filePath = __DIR__ .'/tmp.csv'; // for instance current folder
$fh = fopen($filePath, 'w+');
while($row = $result->fetch_assoc()) {
    fputcsv($fh, $row);
}
fclose($fh); 
然后将查找结果发送到客户端:

header('Content-type: text/csv');
header('Content-disposition: attachment;filename=file.csv');

readfile($filePath);

请参见

您可以将数据流式传输到客户端,如下所示:

header('Content-type: text/csv');
header('Content-disposition: attachment;filename=file.csv');

$stdout = fopen('php://stdout', 'w');
while($row = $result->fetch_assoc()) {
    fputcsv($stdout, $row);
}
fclose($stdout);
或写入文件:

$filePath = __DIR__ .'/tmp.csv'; // for instance current folder
$fh = fopen($filePath, 'w+');
while($row = $result->fetch_assoc()) {
    fputcsv($fh, $row);
}
fclose($fh); 
然后将查找结果发送到客户端:

header('Content-type: text/csv');
header('Content-disposition: attachment;filename=file.csv');

readfile($filePath);

请参见

该库已经死了,但是,Github页面上有一个链接,指向一个更新的、活动的替代品-phpsReadSheet。这个库已经死了,但是,Github页面上有一个链接,指向一个更新的、活动的替代品——PHPSReadSheet。