You are here:  » Export data to csv revisited


Export data to csv revisited

Submitted by dflsports on Wed, 2013-12-04 22:39 in

Should the php snippet at the end of this old thread work with the latest version of PT?

http://www.pricetapestry.com/node/506

I tried, but the export file is empty.

I need to export the data imported into PT into a csv or xml file.

Thanks!

Submitted by support on Thu, 2013-12-05 09:03

Hi dflsports,

Have a go with the following which uses an unbuffered query (so no memory constraints) instead of database_querySelect(). For a large database, there may have been insufficient memory to save the result set using the basic version from node 506...

<?php
  set_time_limit
(0);
  require(
"includes/common.php");
  
header("Content-Type: application/octet-stream");
  
header("Content-Disposition: attachment; filename=products.csv");
  
$link = @mysql_connect($config_databaseServer,$config_databaseUsername,$config_databasePassword);
  @
mysql_select_db($config_databaseName,$link);
  
$sql "SELECT * FROM `".$config_databaseTablePrefix."products`";
  
$result mysql_unbuffered_query($sql,$link);
  print 
"Merchant,Name,Description,ImageURL,BuyURL,Price,Category,Brand\n";
  
$search = array("\n","\r",",");
  while(
$row mysql_fetch_array($result,MYSQL_ASSOC))
  {
    foreach(
$row as $k => $v)
    {
      
$row[$k] = str_replace($search," ",$v);
    }
    print 
$row["merchant"].",";
    print 
$row["name"].",";
    print 
$row["description"].",";
    print 
$row["image_url"].",";
    print 
$row["buy_url"].",";
    print 
$row["price"].",";
    print 
$row["category"].",";
    print 
$row["brand"]."\n";
  }
?>

Cheers,
David.
--
PriceTapestry.com

Submitted by robdeg on Tue, 2015-12-08 10:23

Hello David,

How can i select a merchant to export data for?

For example export data for merchant1,merchant4 etc.

Thanks!

Submitted by support on Tue, 2015-12-08 10:55

Hi,

To hard code the required merchant, you can add a WHERE clause to the SELECT SQL, so where you have the following code at line 8 of the above script:

  $sql = "SELECT * FROM `".$config_databaseTablePrefix."products`";

...REPLACE with:

  $sql = "SELECT * FROM `".$config_databaseTablePrefix."products` WHERE merchant='Merchant Name'";

If you wanted to pass the merchant name as a parameter in the URL, use:

  $sql = "SELECT * FROM `".$config_databaseTablePrefix."products` WHERE merchant='".database_safe($_GET["merchant"])."'";

...and then in the request URL, add:

?merchant=Merchant+Name

("+" is the URL encoding for SPACE)

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by sirmanu on Sat, 2019-11-23 17:41

Hi.
Can you update the code for PHP 7? This functions are deprecated.

Also, how can I modify the script to save the CSV on the server rather than download?

Thanks!

Submitted by support on Mon, 2019-11-25 11:10

Hi,

Here's the above updated for mysqli_* functions;

<?php
  set_time_limit
(0);
  require(
"includes/common.php");
  
header("Content-Type: application/octet-stream");
  
header("Content-Disposition: attachment; filename=products.csv");
  
$link mysqli_connect($config_databaseServer,$config_databaseUsername,$config_databasePassword,$config_databaseName);
  
$sql "SELECT * FROM `".$config_databaseTablePrefix."products`";
  
mysqli_real_query($link,$sql);
  
$result mysqli_use_result($link);
  print 
"Merchant,Name,Description,ImageURL,BuyURL,Price,Category,Brand\n";
  
$search = array("\n","\r",",");
  while(
$row mysqli_fetch_assoc($result))
  {
    foreach(
$row as $k => $v)
    {
      
$row[$k] = str_replace($search," ",$v);
    }
    print 
$row["merchant"].",";
    print 
$row["name"].",";
    print 
$row["description"].",";
    print 
$row["image_url"].",";
    print 
$row["buy_url"].",";
    print 
$row["price"].",";
    print 
$row["category"].",";
    print 
$row["brand"]."\n";
  }
?>

Without modification you could use redirection to save to the server e.g. as a cron job or from the command line using;

cd /path/to/scripts/;php export.php > ../feeds/export.csv

So a change of directory to the scripts folder; followed by the command using ">" to redirect stdout to the file ../feeds/export.csv. If you would prefer the script to write to a file directly just let me know...

Cheers,
David.
--
PriceTapestry.com

Submitted by sirmanu on Fri, 2019-11-29 10:57

Awesome! Thank you very much, it works as expected