Php method for exporting MySQL data to a CSV file using the specified encoding _php tutorial

Source: Internet
Author: User

Php method for exporting MySQL data to a CSV file using the specified encoding


This example describes how PHP uses the specified encoding to export MySQL data to a CSV file. Share to everyone for your reference. The 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

21st

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 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 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";

}

?>

http://www.bkjia.com/PHPjc/977164.html www.bkjia.com true http://www.bkjia.com/PHPjc/977164.html techarticle PHP using the specified encoding to export MySQL data to a CSV file The example in this article describes how PHP uses the specified encoding to export MySQL data to a CSV file. Share to everyone for your reference. Specific ...

  • 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.