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

问题描述:

I am trying to export a MySQL database using PHP, I want the data to be saved to the users computer, I want this data to be saved as a .sql file. I am trying to make it as simple as phpmyadmin.

I have tried everything I can find on Google.

I have tried to do mysql dumps, custom scripts, but they all write the data to the server, never the client computer.

All help is appreciated!

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

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

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

感谢所有帮助! p> div>

You need to use header() to set an appropriate content type and output the data.

Normal output is sent to the user as Content-Type: text/html

If you want to send output as a text document, use:

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

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