SELECT ... INTO OUTFILE 'result.csv' 之后如何保存文件

发布于 2024-10-04 18:53:52 字数 235 浏览 2 评论 0原文

我使用此查询将表转储到 csv 文件中:

$sql = "SELECT * 
        INTO OUTFILE 'result.csv'
        FIELDS TERMINATED BY ';'
        FROM tableName";

我在 mysql 的 db 文件夹中得到一个 result.csv 文件,

如何将其保存在我的网站的根目录下?

i use this query to dump table into csv file :

$sql = "SELECT * 
        INTO OUTFILE 'result.csv'
        FIELDS TERMINATED BY ';'
        FROM tableName";

i get a result.csv file in the db folder of mysql

how can i save it at root of my site ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

赠佳期 2024-10-11 18:53:52

提供您希望保存结果的路径来代替 result.csv

$sql = "SELECT * 
        INTO OUTFILE '/path/to/your/site/result.csv'
        FIELDS TERMINATED BY ';'
        FROM tableName";

In place of result.csv provide the path where you want it to be saved:

$sql = "SELECT * 
        INTO OUTFILE '/path/to/your/site/result.csv'
        FIELDS TERMINATED BY ';'
        FROM tableName";
孤单情人 2024-10-11 18:53:52

这段代码可以将mysql表导出到csv文件中,我已经用大表对其进行了测试

<?php 
// tested with success
$db_name     = "db_name";
$db_password = "pass";
$db_link     = mysql_connect("localhost", "root", $db_password);
mysql_select_db($db_name, $db_link);
mysql_query("SET NAMES UTF8");

$table = "table_name";

function assoc_query_2D($sql, $id_name = false){
  $result = mysql_query($sql);
  $arr = array();
  $row = array();
  if($result){
    if($id_name == false){
      while($row = mysql_fetch_assoc($result))
        $arr[] = $row;
    }else{
      while($row = mysql_fetch_assoc($result)){
        $id = $row['id'];
        $arr[$id] = $row;
      }
    }
  }else
      return 0;

  return $arr;
}

function query_whole_table($table, $value = '*'){
    $sql = "SELECT $value FROM $table";
  return assoc_query_2D($sql);
}

$export_str = "";
$result = query_whole_table($table);

foreach($result as $record){
  $export_str .= implode(";",$record) . "\n";
}
// add time to fileName
$date = time(); 
// output the file 
// we can set a header to send it directly to the browser
file_put_contents($date.'_'.$table."_export.csv", $export_str);
?>

this code can export mysql table into csv file , i've tested it with large table

<?php 
// tested with success
$db_name     = "db_name";
$db_password = "pass";
$db_link     = mysql_connect("localhost", "root", $db_password);
mysql_select_db($db_name, $db_link);
mysql_query("SET NAMES UTF8");

$table = "table_name";

function assoc_query_2D($sql, $id_name = false){
  $result = mysql_query($sql);
  $arr = array();
  $row = array();
  if($result){
    if($id_name == false){
      while($row = mysql_fetch_assoc($result))
        $arr[] = $row;
    }else{
      while($row = mysql_fetch_assoc($result)){
        $id = $row['id'];
        $arr[$id] = $row;
      }
    }
  }else
      return 0;

  return $arr;
}

function query_whole_table($table, $value = '*'){
    $sql = "SELECT $value FROM $table";
  return assoc_query_2D($sql);
}

$export_str = "";
$result = query_whole_table($table);

foreach($result as $record){
  $export_str .= implode(";",$record) . "\n";
}
// add time to fileName
$date = time(); 
// output the file 
// we can set a header to send it directly to the browser
file_put_contents($date.'_'.$table."_export.csv", $export_str);
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文