Created
January 18, 2020 02:40
-
-
Save NickGreen/af8e5f7a0da27a4b38e34e8d4536ebb2 to your computer and use it in GitHub Desktop.
export Points and Rewards custom table to CSV
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//DB details | |
$dbHost = 'localhost'; | |
$dbUsername = 'root'; | |
$dbPassword = 'root'; | |
$dbName = 'local'; | |
$dbPrefix = 'wp'; | |
//Create connection and select DB | |
$db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName); | |
if($db->connect_error){ | |
die("Unable to connect database: " . $db->connect_error); | |
} | |
//get records from database | |
$query = $db->query("SELECT * FROM " . $dbPrefix . "_wc_points_rewards_user_points"); | |
if($query->num_rows > 0){ | |
$delimiter = ","; | |
$filename = "points_" . date('Y-m-d') . ".csv"; | |
//create a file pointer | |
$f = fopen('php://memory', 'w'); | |
//set column headers | |
$fields = array('user', 'user_id', 'points', 'points_balance', 'order_id', 'date'); | |
fputcsv($f, $fields, $delimiter); | |
//output each row of the data, format line as csv and write to file pointer | |
while( $row = $query->fetch_assoc() ) { | |
$lineData = array( $row['id'], $row['user_id'], $row['points'], $row['points_balance'], $row['order_id'], $row['date'], ); | |
fputcsv( $f, $lineData, $delimiter ); | |
} | |
//move back to beginning of file | |
fseek($f, 0); | |
//set headers to download file rather than displayed | |
header('Content-Type: text/csv'); | |
header('Content-Disposition: attachment; filename="' . $filename . '";'); | |
//output all remaining data on a file pointer | |
fpassthru($f); | |
} | |
exit; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment