简体   繁体   English

是否可以将搜索数据导出到csv文件中

[英]is it possible to export the search data into a csv file

im working on php with mysql and there is a search form in my site by which a user can search by name and location from mysql database.so i have a table in database named customers which have 5 fields name, enail,mobile,address and location.when a user search the result appear in table format.now i want to export this result in csv format.is it possible?? 我在用mysql的php上工作,我的网站上有一个搜索表单,用户可以通过它从mysql database.name中搜索名称和位置,因此我在数据库中有一个名为customers的表,该表具有5个字段,名称,enail,mobile,address和location.when用户搜索结果以表格格式出现。现在我要以csv格式导出此结果。有可能吗?

Here is my search form: 这是我的搜索表:

<form class="form-horizontal" action="" method="post" name="userform">
    <?php if($_GET[id]){?>
    <?php }else{?>
    <fieldset>
     <legend>Search</legend>
    <div class="control-group">
    <label class="control-label">Search Term</label>
    <div class="controls">
    <input type="text" class="span3 search-query" name="term" placeholder="search by name and location">
    <button type="submit" class="btn" name="search">Search</button>
    </div>
    </div>
</form>

and Here is the query to fetch the result from databse: 这是从数据库获取结果的查询:

<?php 
                 // to print the records
                $term = $_POST['term'];
                if($term != ""){
                 $sql = mysql_query("select * from customers where name like '%$term%' or location like '%$term%'");
                while ($row = mysql_fetch_array($sql)){
                ?>

                <tr>
                <td><?php echo $row[cid];?></td>
                <td><?php echo $row[name ];?></td>
                <td><?php echo $row[email];?></td>
                <!--<td>&nbsp;</td>-->
                <td><?php echo $row[mobile];?></td>               
                <td><?php echo $row[address];?></td>
                 <td><?php echo $row[location];?></td>
?>

Maybe you query the result again like this: 也许您再次像这样查询结果:

$query = "SELECT * 
INTO OUTFILE '/tmp/result.csv'
FIELDS TERMINATED BY ',' 
OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '\\'
LINES TERMINATED BY '\n'
FROM customers
WHERE name LIKE '%" . $term . "%'
OR location LIKE '%" . $term . "%'";

Haven't tried this yet, but this should generate a csv file you can then open or link to. 还没有尝试过,但这应该会生成一个csv文件,然后可以打开或链接到该文件。

PHPExcel has already been suggested and I concur, this is one of the best libraries out there. 已经建议使用PHPExcel,我同意,这是目前最好的库之一。 One major drawback is its memory footprint, and it could be an overkill for a simple CSV export. 一个主要的缺点是它的内存占用量,对于简单的CSV导出来说可能是一个过大的杀伤力。

If you only need CSV, you may want to look into the (more low-level) built-in feature of MySQL: SELECT ... INTO OUTFILE . 如果只需要CSV,则可能需要研究MySQL的(更底层的)内置功能: SELECT ... INTO OUTFILE

It is less flexible and has a few pitfalls, but its is lightning fast compared to resorting to an external library. 它不那么灵活,有一些陷阱,但是与诉诸外部库相比,它闪电般快。 Most problems you may encounter with this approach have already been answered here at StackOverflow. 使用此方法可能遇到的大多数问题已在此处的StackOverflow中得到了解答。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM