Php uses the specified encoding to export mysql Data to a csv file

Source: Internet
Author: User

Php uses the specified encoding to export mysql Data to a csv file

This example describes how php exports mysql Data to a csv file using the specified encoding. Share it with you for your reference. The specific implementation method is as follows:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

<? Php

/*

* PHP code to export MySQL data to CSV

*

* Sends the result of a MySQL query as a CSV file for download

* Easy to convert to UTF-8.

*/

 

/*

* Establish database connection

*/

 

$ Conn = mysql_connect ('localhost', 'login', 'pass') or die (mysql_error ());

Mysql_select_db ('database _ name', $ conn) or die (mysql_error ($ conn ));

Mysql_query ("set names CP1252 ");

/*

* Execute SQL query

*/

$ Query = sprintf ('select field1, field2 FROM table_name ');

$ Result = mysql_query ($ query, $ conn) or die (mysql_error ($ conn ));

/*

* Send response headers to the browser

* Following headers instruct the browser to treat the data as a csv file called export.csv

*/

Header ('content-Type: text/csv; charset = cp1252 ');

Header ('content-Disposition: attachment=filename=output.csv ');

/*

* Output header row (if atleast one row exists)

*/

 

$ Row = mysql_fetch_assoc ($ result );

If ($ row ){

Echocsv (array_keys ($ row ));

}

 

/*

* Output data rows (if atleast one row exists)

*/

While ($ row ){

Echocsv ($ row );

$ Row = mysql_fetch_assoc ($ result );

}

 

/*

* Echo the input array as csv data maintaining consistency with most CSV implementations

*-Uses double-quotes as enclosure when necessary

*-Uses double-quotes to escape double-quotes

*-Uses CRLF as a line separator

*/

 

Function echocsv ($ fields)

{

$ Separator = '';

Foreach ($ fields as $ field ){

If (preg_match ('/\ r | \ n |, | "/', $ field )){

$ Field = '"'. str_replace ('"', '""', $ field ).'"';

}

Echo $ separator. $ field;

$ Separator = ',';

}

Echo "\ r \ n ";

}

?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.