1<?php
2
3//store the data that you retrieve from DB in a varible, in my case its $keywords_analytics
4
5if($keywords_analytics!=''){
6 $delimiter = ",";
7 $fileName = 'search_terms.csv';
8
9
10
11 // Create a file pointer
12 $f = fopen('php://memory', 'w');
13
14 // Set column headers
15 $fields = array('Date', 'Search Term', 'Total');
16
17 fputcsv($f, $fields, $delimiter);
18
19 foreach ($keywords_analytics as $ka){
20 $lineData = array($ka['dated'], $ka['keyword'], $ka['total']);
21 fputcsv($f, $lineData, $delimiter);
22 }
23 // Move back to beginning of file
24 fseek($f, 0);
25
26 // Set headers to download file rather than displayed
27 header('Content-Type: text/csv');
28 header('Content-Disposition: attachment; filename="' . $fileName . '";');
29
30 //output all remaining data on a file pointer
31 fpassthru($f);
32}
33exit;
34?>
1<?php
2/*
3* iTech Empires: Export Data from MySQL to CSV Script
4* Version: 1.0.0
5* Page: Index
6*/
7
8// Database Connection
9require("db_connection.php");
10
11// List Users
12$query = "SELECT * FROM users";
13if (!$result = mysqli_query($con, $query)) {
14 exit(mysqli_error($con));
15}
16
17if (mysqli_num_rows($result) > 0) {
18 $number = 1;
19 $users = '<table class="table table-bordered">
20 <tr>
21 <th>No.</th>
22 <th>First Name</th>
23 <th>Last Name</th>
24 <th>Email</th>
25 </tr>
26 ';
27 while ($row = mysqli_fetch_assoc($result)) {
28 $users .= '<tr>
29 <td>'.$number.'</td>
30 <td>'.$row['first_name'].'</td>
31 <td>'.$row['last_name'].'</td>
32 <td>'.$row['email'].'</td>
33 </tr>';
34 $number++;
35 }
36 $users .= '</table>';
37}
38
39?>
40<!doctype html>
41<html lang="en">
42<head>
43 <meta charset="UTF-8">
44 <title>Export Data from MySQL to CSV Tutorial | iTech Empires</title>
45 <!-- Bootstrap CSS File -->
46 <link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.min.css"/>
47</head>
48<body>
49<div class="container">
50 <!-- Header -->
51 <div class="row">
52 <div class="col-md-12">
53 <h2>Export Data from MySQL to CSV</h2>
54 </div>
55 </div>
56 <!-- /Header -->
57
58 <!-- Content -->
59 <div class="form-group">
60 <?php echo $users ?>
61 </div>
62 <div class="form-group">
63 <button onclick="Export()" class="btn btn-primary">Export to CSV File</button>
64 </div>
65 <!-- /Content -->
66
67 <script>
68 function Export()
69 {
70 var conf = confirm("Export users to CSV?");
71 if(conf == true)
72 {
73 window.open("export.php", '_blank');
74 }
75 }
76 </script>
77</div>
78</body>
79</html>
80
81
1<?php
2/*
3* iTech Empires: Export Data from MySQL to CSV Script
4* Version: 1.0.0
5* Page: DB Connection
6*/
7
8// Connection variables
9$host = "localhost"; // MySQL host name eg. localhost
10$user = "root"; // MySQL user. eg. root ( if your on localserver)
11$password = ""; // MySQL user password (if password is not set for your root user then keep it empty )
12$database = "test"; // MySQL Database name
13
14// Connect to MySQL Database
15$con = new mysqli($host, $user, $password, $database);
16
17// Check connection
18if ($con->connect_error) {
19 die("Connection failed: " . $con->connect_error);
20}
21?>
22