Author
This class can export MySQL query results as a CSV file.
It can execute a given SQL query using the MySQL command line shell program to save the query results as a CSV file to avoid exceeding the PHP memory limit, especially when the query results return over 30,000 records.
The package can serve the generated CSV file to let users download it.
If you are facing difficulty in performing large MySQL query export to CSV operation in PHP; then this is the class for you.
define('HOSTNAME', '127.0.0.1');
define('USERNAME', 'username');
define('PASSWORD', 'password');
define('DATABASE', 'database');
$sql = "
SELECT
column1 as COLUMN1,
column2 as COLUMN2,
column3 as COLUMN3,
column4 as COLUMN4
FROM
TABLE_NAME
WHERE
column5 = :column5
column6 LIKE CONCAT('%' , :column6, '%');
column7 IN (:column7);
";
$params = [
':column5' => 'column5_value',
':column6' => 'column6_search_value',
':column7' => [
'column7_value1',
'column7_value2',
'column7_value3'
]
];
$csvFilename = 'export.csv';
try {
$mySqlCsv = new downloadCSV();
$mySqlCsv->connect(HOSTNAME, USERNAME, PASSWORD, DATABASE);
$mySqlCsv->useTmpFile = false; // defaults true for large data export.
$mySqlCsv->initDownload($csvFilename, $sql, $params);
} catch (\Exception $e) {
echo $e->getMessage();
}
Export / Download MySQL query results as a CSV file
When it comes to Download CSV, most of the time developer faces issue of memory limit in PHP; especially when supporting downloads of more than 30,000 records at a time.
Below class solves this issue by executing shell command for MySql Client installed on the server from PHP script. Using this class one can download all the records in one go. There is no limit to number of rows returned by SQL query.<br>
<?php
require_once __DIR__ . '/AutoloadExportCSV.php';
use ExportCSV\ExportCSV;
define(constant_name: 'HOSTNAME', value: '127.0.0.1');
define(constant_name: 'USERNAME', value: 'username');
define(constant_name: 'PASSWORD', value: 'password');
define(constant_name: 'DATABASE', value: 'database');
$sql = "
SELECT
column1 as COLUMN1,
column2 as COLUMN2,
column3 as COLUMN3,
column4 as COLUMN4
FROM
TABLE_NAME
WHERE
column5 = :column5
column6 LIKE CONCAT('%' , :column6, '%');
column7 IN (:column7);
";
$params = [
':column5' => 'column5_value',
':column6' => 'column6_search_value',
':column7' => [
'column7_value1',
'column7_value2',
'column7_value3'
]
];
$csvFilename = 'export.csv';
try {
$exportCSV = new ExportCSV(dbType: 'MySQL');
$exportCSV->connect(
hostname: HOSTNAME,
username: USERNAME,
password: PASSWORD,
database: DATABASE
);
$exportCSV->useTmpFile = false; // defaults true for large data export.
$exportCSV->initDownload(
csvFilename: $csvFilename,
sql: $sql,
params: $params
);
} catch (\Exception $e) {
echo $e->getMessage();
}
<?php
require_once __DIR__ . '/AutoloadExportCSV.php';
use ExportCSV\ExportCSV;
define(constant_name: 'HOSTNAME', value: '127.0.0.1');
define(constant_name: 'USERNAME', value: 'username');
define(constant_name: 'PASSWORD', value: 'password');
define(constant_name: 'DATABASE', value: 'database');
$sql = "
SELECT
column1 as COLUMN1,
column2 as COLUMN2,
column3 as COLUMN3,
column4 as COLUMN4
FROM
TABLE_NAME
WHERE
column5 = :column5
column6 LIKE CONCAT('%' , :column6, '%');
column7 IN (:column7);
";
$params = [
':column5' => 'column5_value',
':column6' => 'column6_search_value',
':column7' => [
'column7_value1',
'column7_value2',
'column7_value3'
]
];
$csvAbsoluteFilePath = '/<folder path>/<filename>.csv';
try {
$exportCSV = new ExportCSV(dbType: 'MySQL');
$exportCSV->connect(
hostname: HOSTNAME,
username: USERNAME,
password: PASSWORD,
database: DATABASE
);
$exportCSV->saveCsvExport(
sql: $sql,
params: $params,
csvAbsoluteFilePath: $csvAbsoluteFilePath
);
} catch (\Exception $e) {
echo $e->getMessage();
}
<?php
require_once __DIR__ . '/AutoloadExportCSV.php';
use ExportCSV\ExportCSV;
define(constant_name: 'HOSTNAME', value: '127.0.0.1');
define(constant_name: 'USERNAME', value: 'username');
define(constant_name: 'PASSWORD', value: 'password');
define(constant_name: 'DATABASE', value: 'database');
$sql = "
SELECT
column1 as COLUMN1,
column2 as COLUMN2,
column3 as COLUMN3,
column4 as COLUMN4
FROM
TABLE_NAME
WHERE
column5 = :column5
column6 LIKE CONCAT('%' , :column6, '%');
column7 IN (:column7);
";
$params = [
':column5' => 'column5_value',
':column6' => 'column6_search_value',
':column7' => [
'column7_value1',
'column7_value2',
'column7_value3'
]
];
$csvFilename = 'data.csv';
$csvAbsoluteFilePath = '/<folder path>/<filename>.csv';
try {
$exportCSV = new ExportCSV(dbType: 'MySQL');
$exportCSV->connect(
hostname: HOSTNAME,
username: USERNAME,
password: PASSWORD,
database: DATABASE
);
$exportCSV->initDownload(
csvFilename: $csvFilename,
sql: $sql,
params: $params,
csvAbsoluteFilePath: $csvAbsoluteFilePath
);
} catch (\Exception $e) {
echo $e->getMessage();
}
To enable compression for downloading dynamically generated CSV files in NGINX if the browser supports compression, one can use the gzip\_types directive in the NGINX configuration file.
The gzip\_types directive is used to specify which MIME types should be compressed.
Here's an example of how you can enable compression for downloading dynamically generated CSV files in NGINX:
http {
# ...
gzip on;
gzip_types text/plain text/csv;
# ...
}
File | Role | Description | ||
---|---|---|---|---|
Files folder imageExportCSV (4 files) | ||||
Plain text file Autoload.php | Class | Class source | ||
Accessible without login Plain text file AutoloadExportCSV.php | Aux. | Configuration script | ||
Accessible without login Plain text file Config.php | Aux. | Configuration script | ||
Accessible without login Plain text file Example.php | Example | Example script | ||
Accessible without login Plain text file LICENSE | Lic. | License text | ||
Accessible without login Plain text file README.md | Doc. | Documentation |
File | Role | Description |
---|---|---|
Plain text file DB.php | Class | Class source |
Plain text file DBInterface.php | Class | Class source |
Plain text file ExportCSV.php | Class | Class source |
Plain text file MySQL.php | Class | Class source |
Add link image If you know an application of this package, send a message to the author to add a link here.