[英]Export php to csv
I would like to ask if it is possible that before exporting php data to csv it would be query first or filtered(ex: searching a name)? 我想问一下是否有可能在将php数据导出到csv之前先查询还是过滤(例如:搜索名称)? I have a code in exporting php data to csv but I would like it to be filtered first.
我在将php数据导出到csv中有一个代码,但我希望先对其进行过滤。
This is my code of exporting php data to csv. 这是我将php数据导出到csv的代码。
<?php
// call export function
exportMysqlToCsv('export_csv.csv');
// export csv
function exportMysqlToCsv($filename = 'export_csv.csv')
{
$conn = dbConnection();
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql_query = "SELECT id, name, code FROM toy";
// Gets the data from the database
$result = $conn->query($sql_query);
$f = fopen('php://temp', 'wt');
$first = true;
while ($row = $result->fetch_assoc()) {
if ($first) {
fputcsv($f, array_keys($row));
$first = false;
}
fputcsv($f, $row);
} // end while
$conn->close();
$size = ftell($f);
rewind($f);
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Length: $size");
// Output to browser with appropriate mime type, you choose ;)
header("Content-type: text/x-csv");
header("Content-type: text/csv");
header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=$filename");
fpassthru($f);
exit;
}
// db connection function
function dbConnection(){
$servername = "localhost";
$username = "root";
$password = "louchin";
$dbname = "phppot_examples";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
return $conn;
}
?>
Please help me and suggest if possible. 请帮助我,并提出可能的建议。 Thank you!
谢谢!
The data is called up in the SQL query: 在SQL查询中调用数据:
$sql_query = "SELECT id, name, code FROM toy";
I think you can just add a WHERE
clause to this to add your filters. 我认为您可以在此添加
WHERE
子句以添加过滤器。
A quick Google for 'MySQL WHERE' will give you plenty of places you can look up the syntax. 对于“ MySQL WHERE”的快速Google搜索将为您提供很多查找语法的地方。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.