如何在PHP中运行MySQL查询并将数据发送回浏览器?

时间:2013-05-24 19:26:04

标签: php mysql

我正在尝试使用PHP导出MySQL数据库,我希望将数据保存到用户计算机,我希望将这些数据保存为.sql文件。我想让它像phpmyadmin一样简单。

我已尝试过在Google上找到的所有内容。

我曾尝试过mysql转储,自定义脚本,但它们都将数据写入服务器,而不是客户端计算机。

感谢所有帮助!

2 个答案:

答案 0 :(得分:2)

您需要使用header()来设置适当的内容类型并输出数据。

正常输出以Content-Type: text/html

的形式发送给用户

如果要将输出作为文本文档发送,请使用:

header('Content-type: text/plain');
header('Content-Disposition: attachment; filename="yourfilename.sql"');
echo $your_sql_content;

答案 1 :(得分:1)

<?php

header("Content-type: text/plain");
header("Content-Disposition: attachment; filename=YOUR_EXPORT.sql");
header("Pragma: no-cache");
header("Expires: 0");
$fp = fopen('php://output', 'w');

$your_query = mysql_query( "select * from bla bla bla ... ");
while( $codes = mysql_fetch_array( $your_query ) ) {
    $row = array();
    $row[] = some data ... 
    fputs($fp, $row);
}
fclose($fp);
相关问题