如何从MySQL数据库中表的列中导出.txt文件? [英] How to export a .txt file from a column of a table in MySQL database?

查看:284
本文介绍了如何从MySQL数据库中表的列中导出.txt文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想逐行导出.txt格式的电话号码.数据库saiautocare中的表名是customers,列名是customer_contact_numbers我正在使用Laravel框架,JQuery和MySQL数据库.

I want to export the phone numbers in .txt format line by line. Table name is customers and the column name is customer_contact_numbers from the database saiautocare I am using the Laravel framework, JQuery and MySQL database.

当我单击该按钮时,它将询问我将文件保存在何处并将数字逐行导出到文本文件中.

When I click that button it will ask me where to save the file and export the numbers line by line into the text file.

代码如下:

      <div class="col-sm-6 col-lg-3">
    <div class="card text-white bg-primary">
      <div class="card-body pb-0">
        <div class="btn-group float-right">
          <button type="button" class="btn btn-transparent dropdown-toggle p-0" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
            <i class="icon-settings"></i>
          </button>
          <div class="dropdown-menu dropdown-menu-right">
            <a class="dropdown-item" href="#">Export to TXT</a>
          </div>
        </div>
        <h4 class="mb-0">{{ $TotalCustomers }}</h4> 
        <p>Marketing Numbers</p>
      </div>
      <div class="chart-wrapper px-3" style="height:70px;">
        <canvas id="card-chart1" class="chart" height="70"></canvas>
      </div>
    </div>
  </div>

我是PHP和MySQL的新手,这就是为什么我无法提供所有必需信息的原因.

I'm new to PHP and MySQL, that's why I'm sorry if I am not able to provide all the required information.

推荐答案

$customers = Customer::all();
$phoneNumbers = "Phone numbers \n";
foreach ($customers as  $customer) {
  $content .= $customer->customer_contact_numbers;
  $content .= "\n";
}

// file name to download
$fileName = "contact_numbers.txt";

// make a response, with the content, a 200 response code and the headers
return Response::make($content, 200, [
  'Content-type' => 'text/plain', 
  'Content-Disposition' => sprintf('attachment; filename="%s"', $fileName),
  'Content-Length' => sizeof($content)
];);

这篇关于如何从MySQL数据库中表的列中导出.txt文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆